[英]Generic Repository Pattern in Entity Framework
我是通用存儲庫模式的新手。 我試圖創建一個使用通用存儲庫添加,更新,刪除和查找方法的示例。 找到我的示例代碼,
通用存儲庫接口和類:
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();
}
我可以使用SampleRepositoryEntities中的“ TestingTable”表中的Find方法查找記錄。 因為此表的記錄很少,大約10條記錄。
但是我試圖從tbluser表中找到與電子郵件ID匹配的第一條記錄,該表有50,000條以上的記錄,此時我無法保持加載的結果,也沒有任何異常。 我做錯了..誰能讓我解決這個問題?
每當我用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。 使用良好的舊羅斯文數據庫,我發現了以下內容:
用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>
來解釋您遇到的問題。 Func<T, bool>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.