实体框架中的通用存储库模式

时间:2013-01-10 04:27:47

标签: c# entity-framework repository-pattern

我是Generic Repository模式的新手。我尝试使用通用存储库创建一个添加,更新,删除和查找方法的示例。找到我的示例代码,

通用存储库接口和类:

public interface IRepository<T> : IDisposable where T : class
{        
    IEnumerable<T> Find(Func<T, bool> predicate);
    void Add(T entity);
    void SaveChanges();
}

public class DataRepository<T> : IRepository<T> where T : class
{
    private ObjectContext _context;
    private IObjectSet<T> _objectSet;

    public DataRepository(ObjectContext context)
    {
       _context = context;
       _objectSet = _context.CreateObjectSet<T>();
    }
    public IEnumerable<T> Find(Func<T, bool> predicate)
    {
        return _objectSet.Where(predicate);
    }
    public void Add(T entity)
    {
       _objectSet.AddObject(entity);
    }
}

我使用下面的方法,

DataRepository<tblUser> _tblUser = new DataRepository<tblUser>(new SampleRepositoryEntities());
DataRepository<TestingTable> sampleRepository = new DataRepository<TestingTable>(new SampleRepositoryEntities());
public void GetRecords()
{
    var record1 = sampleRepository.Find(f => f.id == 1).FirstOrDefault();
    var record = _tblUser.Find(f => f.emailid == "karthik@abc.com").FirstOrDefault();
}

我可以使用表格中的Find方法找到记录&#34; TestingTable&#34;在SampleRepositoryEntities中。因为这个表记录了大约10条记录。

但我试图找到与tbluser表中的电子邮件ID匹配的第一条记录,此表有超过50,000条记录,我无法在此时保持加载结果并且没有任何例外也。我做错了什么..有人可以让我清楚这个吗?

1 个答案:

答案 0 :(得分:6)

每当我使用EF实现通用存储库时,我使用Expression<Func<T, bool>>而不是Func<T, bool>作为谓词。

我认为在您的情况下发生的事情是在应用谓词之前检索所有50,000条记录。相反,只需改变

public IEnumerable<T> Find(Func<T, bool> predicate)

public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)

并查看是否可以提高效果。

修改

为了扩展@ashutoshraina的观点,我决定测试这两种方法来查看生成的SQL。使用好的旧Northwind数据库,我发现了以下内容:

使用Where(Func< T, bool>)

查询
using (ConsoleApplication2.NorthwindEntities entities =
    new ConsoleApplication2.NorthwindEntities())
{
    Func<Product, bool> f = (p => p.Discontinued);
    var result = entities.Products.Where(f).ToList();
}

生成以下SQL

SELECT  [Extent1].[ProductID] AS [ProductID],
    [Extent1].[ProductName] AS [ProductName],
    [Extent1].[SupplierID] AS [SupplierID],
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[QuantityPerUnit] AS [QuantityPerUnit],
    [Extent1].[UnitPrice] AS [UnitPrice],
    [Extent1].[UnitsInStock] AS [UnitsInStock],
    [Extent1].[UnitsOnOrder] AS [UnitsOnOrder],
    [Extent1].[ReorderLevel] AS [ReorderLevel],
    [Extent1].[Discontinued] AS [Discontinued]
    FROM [dbo].[Products] AS [Extent1]

使用Where(Expression<Func< T, bool>>)

查询
using (ConsoleApplication2.NorthwindEntities entities =
    new ConsoleApplication2.NorthwindEntities())
{
    Expression<Func<Product, bool>> f2 = (p => p.Discontinued);
    var result2 = entities.Products.Where(f2).ToList();
}

生成以下SQL:

SELECT  [Extent1].[ProductID] AS [ProductID],
    [Extent1].[ProductName] AS [ProductName],
    [Extent1].[SupplierID] AS [SupplierID],
    [Extent1].[CategoryID] AS [CategoryID],
    [Extent1].[QuantityPerUnit] AS [QuantityPerUnit],
    [Extent1].[UnitPrice] AS [UnitPrice],
    [Extent1].[UnitsInStock] AS [UnitsInStock],
    [Extent1].[UnitsOnOrder] AS [UnitsOnOrder],
    [Extent1].[ReorderLevel] AS [ReorderLevel],
    [Extent1].[Discontinued] AS [Discontinued]
    FROM [dbo].[Products] AS [Extent1] 
    WHERE [Extent1].[Discontinued] = 1

这表明使用Expression<Func<T, bool>>方法生成了将谓词考虑在内的查询,从而让SQL Server可以完成更多的工作,这可以解释您使用{{1}时遇到的问题}。