【发布时间】:2010-12-12 18:43:24
【问题描述】:
我目前正在阅读 Pro Asp.Net MVC Framework 一书。在书中,作者建议使用类似于以下的存储库模式。
[Table(Name = "Products")]
public class Product
{
[Column(IsPrimaryKey = true,
IsDbGenerated = true,
AutoSync = AutoSync.OnInsert)]
public int ProductId { get; set; }
[Column] public string Name { get; set; }
[Column] public string Description { get; set; }
[Column] public decimal Price { get; set; }
[Column] public string Category { get; set; }
}
public interface IProductsRepository
{
IQueryable<Product> Products { get; }
}
public class SqlProductsRepository : IProductsRepository
{
private Table<Product> productsTable;
public SqlProductsRepository(string connectionString)
{
productsTable = new DataContext(connectionString).GetTable<Product>();
}
public IQueryable<Product> Products
{
get { return productsTable; }
}
}
然后通过以下方式访问数据:
public ViewResult List(string category)
{
var productsInCategory = (category == null) ? productsRepository.Products : productsRepository.Products.Where(p => p.Category == category);
return View(productsInCategory);
}
这是访问数据的有效方法吗?是要从数据库中检索整个表并在内存中进行过滤,还是链式 Where() 方法会导致一些 LINQ 魔术来创建基于 lambda 的优化查询?
最后,当通过 LINQ-to-SQL 连接时,C# 中存储库模式的其他哪些实现可能会提供更好的性能?
【问题讨论】:
-
当您说“C# 中存储库模式的其他哪些实现可能会提供更好的性能”时,您指的是其他哪些数据访问提供程序?
标签: c# performance linq-to-sql architecture repository-pattern