title: MVC 5 EF6 unit of work and repository pattern
body: <p>How do you implement the unit of work and repository pattern in MVC 5 and EF6? Previously I've avoided any need for unit of work by using a single repository which was injected into my controller as follows:</p>
  
  <pre><code>public class ProductController : BaseController
  {
      private IShopRepository _repository;
  
  
      public ClassController()
          : this(new ShopRepository())
      {
      }
  
      public ClassController(IShopRepository repository)
      {
          _repository = repository;
      }
  
  
      ....
  
  }
  </code></pre>
  
  <p>But now I want to refactor the code so that I have a separate repository for each entity type eg. ProductRepository, CustomerRepository etc and be able to inject multiple repositories into a controller whilst ensuring the same dbcontext is used.</p>
  
  <p>Reading through the microsoft tutorial on <a href="http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/advanced-entity-framework-scenarios-for-an-mvc-web-application" rel="nofollow">http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/advanced-entity-framework-scenarios-for-an-mvc-web-application</a>, the architects now advise the repository and unit of work pattern are no longer needed but they don't provide any example of how to implement or structure repositories in their examples?</p>
  
  <p>Some people have even started renaming repositories to services?</p>
  
  <p>How do you structure your repositories and implement unit of work in MVC5 using EF6 perhaps using an IOC such as Unity? Or what is another solution?</p>
  
  <p>I'm working along the following lines but not sure if it is best solution and how do i add unit of work?</p>
  
  <pre><code>public class ShopContext : DbContext
  {
      public ShopContext() : base("name=ShopContext")
      {
      }
  
      public DbSet&lt;Product&gt; Products { get; set; }
      public DbSet&lt;Customer&gt; Customers { get; set; }
      ...
  
  }
  
  
  public interface IProductRepository
  {
     IEnumerable&lt;Product&gt; GetAll();
     ...
  }
  
  
  public interface ICustomerRepository
  {
     IEnumerable&lt;Customer&gt; GetAll();
     ...
  }
  
  public class ProductRepository : IDisposable, IProductRepository
  {
       private ShopContext _context;
  
       public ProductRepository()
       {
      _context  = new ShopContext();
  
       }
  
       public IEnumerable&lt;Product&gt; GetAll()
       {
          return _context.Products;
       }
  
       // Other methods not displayed
  
       protected void Dispose(bool disposing)
       {
         if (disposing)
         {
             if (_context != null)
             {
                _context.Dispose();
                _context = null;
             }
         }
       }
  
       public void Dispose()
       {
         Dispose(true);
         GC.SuppressFinalize(this);
       }
  
  }
  
  public class CustomerRepository : IDisposable, ICustomerRepository
  {
       private ShopContext _context;
  
       public CustomerRepository()
       {
          _context  = new ShopContext();
  
       }
  
       public IEnumerable&lt;Customer&gt; GetAll()
       {
          return _context.Customers;
       }
  
       // Other methods not displayed
  
       protected void Dispose(bool disposing)
       {
         if (disposing)
         {
             if (_context != null)
             {
                _context.Dispose();
                _context = null;
             }
         }
       }
  
       public void Dispose()
       {
         Dispose(true);
         GC.SuppressFinalize(this);
       }
  }
  
  
  public class ProductsController : BaseController
  {
  
     private IProductRepository _productRepository;
     private ICustomerRepository _customerRepository;
  
  
     public ProductsController()
          : this(new ProductRepository(), new CustomerRepository())
      {
      }
  
     public ProductsController(IProductRepository productRepository, ICustomerRepository customerRepository)  
     {
         _productRepository = productRepository;
         _customerRepository = customerRepository;
  
     }
  
     // Other controller methods not shown.
  } 
  </code></pre>
  
  <p>Example code would be helpful.</p>