title: Best Repository Pattern for ASP.NET MVC
body: <p>I recently learned ASP.NET MVC (I love it). I'm working with a company that uses dependency injection to load a Repository instance in each request, and I'm familiar with using that repository.</p>
  
  <p>But now I'm writing a couple of MVC applications of my own. I don't fully understand the hows and whys of the repository my company uses, and I'm trying to decide the best approach to implement data access.</p>
  
  <p>I am using C# and Entity Framework (with all the latest versions).</p>
  
  <p>I see three general approaches for handling data access.</p>
  
  <ol>
  <li><p>Regular DB context within a using statement each time I access data. This is simple and it works okay. However, if two locations need to read the same data within one request, the data must be read twice. (With a single repository per request, the same instance would be used in both places and I understand the second read would simply return the data from the first read.)</p></li>
  <li><p>A typical <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application">repository pattern</a>. For reasons I don't understand, this typical pattern involves creating a wrapper class for every table used from the database. That seems wrong to me. In fact, since they are implemented also as interfaces, I'd technically be creating two wrapper classes for each table. EF creates tables for me. I don't believe this approach makes sense.</p></li>
  <li><p>There is also a <a href="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle">generic repository pattern</a> where a single repository class is created to serve all entity objects. This makes much more sense to me. But does it make sense to others? Is the link above the best approach?</p></li>
  </ol>
  
  <p>I'd love to get some input from others on this topic. Are you writing your own repository, using one of those above, or doing something different altogether. Please share.</p>