我正在ASP.NETMVC应用程序中实现存储库“模式”。因此,我在做一些阅读来决定是应该通过.GetTable公开整个DataContext(在构造函数中初始化),还是只公开表<T>当性能是最重要的因素时。
当我在谷歌搜索时,我在
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);
}
}