首先使用Entity Framework代码实现存储库的通用方法

时间:2011-11-23 08:59:26

标签: c# entity-framework c#-4.0 entity-framework-4 entity-framework-4.1

我正在使用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)
);

这是我尝试实现目标的最佳方式吗?我试图让它变得尽可能简单。如果有任何示例代码和文章,我将非常感谢。

3 个答案:

答案 0 :(得分:3)

我确信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 :(得分:1)

由于您提到项目可以按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();
    }
}

答案 2 :(得分:1)