title: EF6 Code First with generic repository and Dependency Injection and SoC
body: <p>After a lots of reading and trying things out with <code>Entity Framework</code> latest stable version (6.1.1). </p>
  
  <p>I'm reading lots of contradictions about whether or not to use repositories with <code>EF6</code> or <code>EF</code> in general, because it's <code>DbContext</code> already provides a repository and <code>DbSet</code> the <code>UoW</code>, out of the box.</p>
  
  <p>Let me first explain what my solution contains in terms of project and then I'll comeback at the contradiction. </p>
  
  <p>It has a class library project, and an <code>asp.net-mvc</code> project. The class lib project being the data access and where <code>Migrations</code> are enabled for <code>Code First</code>.</p>
  
  <p>In my class lib project I have a generic repository:</p>
  
  <pre><code>public interface IRepository&lt;TEntity&gt; where TEntity : class
  {
      IEnumerable&lt;TEntity&gt; Get();
  
      TEntity GetByID(object id);
  
      void Insert(TEntity entity);
  
      void Delete(object id);
  
      void Update(TEntity entityToUpdate);
  }
  </code></pre>
  
  <p>And here is the implementation of it:</p>
  
  <pre><code>public class Repository&lt;TEntity&gt; where TEntity : class
  {
      internal ApplicationDbContext context;
      internal DbSet&lt;TEntity&gt; dbSet;
  
      public Repository(ApplicationDbContext context)
      {
          this.context = context;
          this.dbSet = context.Set&lt;TEntity&gt;();
      }
  
      public virtual IEnumerable&lt;TEntity&gt; Get()
      {
          IQueryable&lt;TEntity&gt; query = dbSet;
          return query.ToList();
      }
  
      public virtual TEntity GetByID(object id)
      {
          return dbSet.Find(id);
      }
  
      public virtual void Insert(TEntity entity)
      {
          dbSet.Add(entity);
      }
  
      public virtual void Delete(object id)
      {
          TEntity entityToDelete = dbSet.Find(id);
          Delete(entityToDelete);
      }
  
      public virtual void Update(TEntity entityToUpdate)
      {
          dbSet.Attach(entityToUpdate);
          context.Entry(entityToUpdate).State = EntityState.Modified;
      }
  }
  </code></pre>
  
  <p>And here a few entities:</p>
  
  <pre><code>public DbSet&lt;User&gt; User{ get; set; }
  public DbSet&lt;Order&gt; Orders { get; set; }
  public DbSet&lt;UserOrder&gt; UserOrders { get; set; }
  public DbSet&lt;Shipment&gt; Shipments { get; set; }
  </code></pre>
  
  <p>I don't what to repeat myself but, with <code>EF6</code> you don't pass repositories anymore, but the <code>DbContext</code> instead. So for <code>DI</code> I've set up the following in the <code>asp-net-mvc</code> project using <code>Ninject</code>:</p>
  
  <pre><code>private static void RegisterServices(IKernel kernel)
  {
      kernel.Bind&lt;ApplicationDbContext&gt;().ToSelf().InRequestScope();
  }
  </code></pre>
  
  <p>And this will inject the <code>ApplicationDbContext</code> via constructor injection to upper layer classes where applicable.</p>
  
  <p>Now coming back to the contradiction.</p>
  
  <p>If we don't need a repository anymore because <code>EF</code> already provides that out of the box, how do we do <code>Separation of Concern</code> (abbreviated as SoC in title)?</p>
  
  <p>Now correct me if I'm wrong, but it sounds to me like I just need to do all the data access logic/calculations (like adding, fetching, updating, deleting and some custom logic/calculations here and there (entity specific)) in the <code>asp.net-mvc</code> project, if I don't add a repository.</p>
  
  <p>Any light on this matter is really appreciated.</p>