English 中文(简体)
先采用实体框架法储存库通用方法
原标题:Implementing generic methods for a repository using Entity Framework code first

我正在使用<代码>Entity Framework 4.1和 交存式

I am trying to create methods that will be used throughout most scenarios. I am trying to create a method that brings back records and sorts it according to the order criteria supplied. It can be sorted by 1, 2 or 3 columns. I want this to be specified. I found the following code in the Orchard framework.

在<代码>上 托存接口有以下(我没有其他方法):

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

实施如下:< 编码> 可计算和提炼;T> Fetch (Expression<Func<T, bool>>coment, 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;
     }
}

The 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 guys有其原因(甚至看着编码基础),但我很想知道,与更标准的LINQ/IQueryable-based Solutions相比,它给人什么?

public interface IReposit或y<T>  
{  
    IQueryable<T> All(); 
} 

使用:

var foos = from f in _foosRepos.All()
           where f.Name == "two" || f.Name == "three"
           或derby 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();
    }
}

See also the generic repository implementation that allows you to specify order criteria in this tutorial: http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签