2

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

通用存储库接口和类:

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 方法从 SampleRepositoryEntities 中的“TestingTable”表中找到记录。因为这个表的记录非常少,大约 10 条记录。

但是我试图从 tbluser 表中找到与电子邮件 ID 匹配的第一条记录,该表有超过 50,000 条记录,我无法得到结果,此时继续加载,也没有任何异常。我做错了什么..有人可以让我澄清一下吗?

4

1 回答 1

7

每当我使用 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)

看看这是否能提高性能。

编辑:

为了扩展@ashutoshrina 的观点,我决定测试这两种方法以查看生成的 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 完成更多工作,这往往会解释您在使用Func<T, bool>.

于 2013-01-10T06:06:36.667 回答