I’m implementing the repository “pattern” in my ASP.NET MVC application. So i was doing some reading to decide whether i should expose the entire DataContext (initializing it in the constructor) or just the table via .GetTable<T> when performance is the most important factor.
While doing my googling i ran into the following code at http://www.codeguru.com/csharp/csharp/net30/article.php/c13799. In this code he takes out the table first and then queries on that object. So now im wondering if there is any advantage to this method.
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);
}
}
The only way your repository will be generic enough to be used across all of your applications is by using
DataContext.EntityName is generated by the standard t4 templates and is the least extensible option.