我正在我的ASP.NET MVC应用程序中实现存储库“模式”。因此,当性能是最重要的因素时,我正在做一些阅读,以决定是否应该公开整个DataContext (在构造函数中初始化它),还是只通过.GetTable公开表。
在进行谷歌搜索时,我在http://www.codeguru.com/csharp/csharp/net30/article.php/c13799上遇到了以下代码。在这段代码中,他首先取出表,然后查询该对象。所以现在我想知道这种方法是否有任何优势。
public void PrintWinners()
{
// creates a data context that takes the path of the database
DataContext dc = new DataContext(@"C:\Program Files\
Microsoft SQL Server\MSSQL.1\MSSQL\Data\UCL.mdf");
// retrieves a Table of Winner
Table<Winner> winners = dc.GetTable<Winner>();
// creates a sequence of winners ordered descending by the
// winning year
var result = from w in winners
orderby w.Year descending
select w;
// prints the sequence of winners
foreach (var w in result)
{
Console.WriteLine("{0} {1}, {2}",
w.Year, w.Name, w.Country);
}
}发布于 2010-08-03 23:44:44
要使存储库足够通用,以便在所有应用程序中使用,唯一的方法是使用
DataContext.GetTable<Entity>DataContext.EntityName由标准t4模板生成,是可扩展性最差的选项。
https://stackoverflow.com/questions/3398164
复制相似问题