首先使用实体框架代码实现存储库的泛型方法

本文关键字:存储 实现 泛型方法 代码 框架 实体 | 更新日期: 2023-09-27 18:15:33

我使用Entity Framework 4.1repository pattern

我正在尝试创建将在大多数情况下使用的方法。我正在尝试创建一种方法,带回记录并根据提供的订单标准对其进行排序。它可以按1、2或3列排序。我希望这个是指定的。我在Orchard framework中发现了以下代码。

IRepository interface中,他们有以下方法(我省略了其他方法):

public interface IRepository<T>
{
     IEnumerable<T> Fetch(Expression<Func<T, bool>> predicate);
     IEnumerable<T> Fetch(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order);
}

IEnumerable<T> Fetch(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order);的实现是:

public class Repository<T> : IRepository<T>
{
     public virtual IQueryable<T> Fetch(Expression<Func<T, bool>> predicate)
     {
          return Table.Where(predicate);
     }
     public virtual IQueryable<T> Fetch(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order)
     {
          var orderable = new Orderable<T>(Fetch(predicate));
          order(orderable);
          return orderable.Queryable;
     }
}

Orderable class:

public class Orderable<T>
{
     private IQueryable<T> _queryable;
     public Orderable(IQueryable<T> enumerable)
     {
          _queryable = enumerable;
     }
     public IQueryable<T> Queryable
     {
          get { return _queryable; }
     }
     public Orderable<T> Asc<TKey>(Expression<Func<T, TKey>> keySelector)
     {
          _queryable = _queryable
               .OrderBy(keySelector);
          return this;
     }
     public Orderable<T> Asc<TKey1, TKey2>(Expression<Func<T, TKey1>> keySelector1,
          Expression<Func<T, TKey2>> keySelector2)
     {
          _queryable = _queryable
               .OrderBy(keySelector1)
               .OrderBy(keySelector2);
          return this;
     }
     public Orderable<T> Asc<TKey1, TKey2, TKey3>(Expression<Func<T, TKey1>> keySelector1,
          Expression<Func<T, TKey2>> keySelector2,
          Expression<Func<T, TKey3>> keySelector3)
     {
          _queryable = _queryable
               .OrderBy(keySelector1)
               .OrderBy(keySelector2)
               .OrderBy(keySelector3);
          return this;
     }
     public Orderable<T> Desc<TKey>(Expression<Func<T, TKey>> keySelector)
     {
          _queryable = _queryable
               .OrderByDescending(keySelector);
          return this;
     }
     public Orderable<T> Desc<TKey1, TKey2>(Expression<Func<T, TKey1>> keySelector1,
          Expression<Func<T, TKey2>> keySelector2)
     {
          _queryable = _queryable
               .OrderByDescending(keySelector1)
               .OrderByDescending(keySelector2);
          return this;
     }
     public Orderable<T> Desc<TKey1, TKey2, TKey3>(Expression<Func<T, TKey1>> keySelector1,
          Expression<Func<T, TKey2>> keySelector2,
          Expression<Func<T, TKey3>> keySelector3)
     {
          _queryable = _queryable
               .OrderByDescending(keySelector1)
               .OrderByDescending(keySelector2)
               .OrderByDescending(keySelector3);
          return this;
     }
}

可以这样使用:

var foos = _fooRepos.Fetch(
     f => f.Name == "two" || f.Name == "three",
     o => o.Asc(f => f.Name, f => f.Id)
);

这是我想要达到的最好的方法吗?我正试图使它尽可能简单。我将感谢所有的帮助,如果有任何示例代码和文章。

首先使用实体框架代码实现存储库的泛型方法

我相信Orchard的家伙有他们的理由(没有看过代码库),但我只是想知道它带来的表与一个更"标准"的基于LINQ/iqueryable的解决方案相比?

public interface IRepository<T>  
{  
    IQueryable<T> All(); 
} 

用法:

var foos = from f in _foosRepos.All()
           where f.Name == "two" || f.Name == "three"
           orderby f.Name, f.Id;

var foos = _foosRepos.All()
        .Where(f => f.Name == "two" || f.Name == "three")
        .OrderBy(f => f.Name).ThenBy(f => f.Id);

既然您提到项目可以按1、2或3个字段排序,那么您可以扩展jeroenh的响应,并将其与Orchard的逻辑"合并":

public interface IRepository<T>
{
    IQueryable<T> All();
    IQueryable<T> Sorted(Func<T, object> sort1, Func<T, object> sort2 = null, Func<T, object> sort3 = null);
}
public class Repository<T> : IRepository<T>
{
    public IQueryable<T> All()
    {
        // TODO: Implement real data retrieval
        return new List<T>().AsQueryable();
    }
    public IQueryable<T> Sorted(Func<T, object> sort1, Func<T, object> sort2 = null, Func<T, object> sort3 = null)
    {
        var list = All();
        var res = list.OrderBy(sort1);
        if (sort2 != null)
            res = res.ThenBy(sort2);
        if (sort3 != null)
            res = res.ThenBy(sort3);
        return res.AsQueryable();
    }
}

请参阅允许您在本教程中指定订单标准的通用存储库实现:http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application