views:

33

answers:

1

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);
   }
}
+1  A: 

The only way your repository will be generic enough to be used across all of your applications is by using

DataContext.GetTable<Entity>

DataContext.EntityName is generated by the standard t4 templates and is the least extensible option.

jfar
Alright, forget the repository part for a sec. Is there any difference with regards to performance between the 2?
Fabian
@Fabian Nope, the generated DataContext.MyEntity calls GetTable<MyEntity>
jfar