title: Abstracting EntityFramework.dll reference using Repository/UoW/DI
body: <p>I've been researching best practice for a new project when using EF (5.0 Beta Code First) as my ORM/DAL. Key considerations are testability, loosely coupled design and unit of work/transactional support across repositories.</p>
  
  <p>I understand that within EF DbContext is UoW and DbSet is Repository and in a service layer you can make changes across multiple repositories co-ordinated by the DbContexts SaveChanges() method. Here's my sample configuration:</p>
  
  <pre><code>public interface IMyContext
  {
      IDbSet&lt;Foo&gt; Foos{ get; }
  
      IDbSet&lt;Bar&gt; Bars { get; }
  
      int SaveChanges();
  
      void Dispose();
  }
  
  public class EFMyContext : DbContext, IMyContext
  {
      private IDbSet&lt;Foo&gt; _foos;
      private IDbSet&lt;Bar&gt; _bars;
  
      public EFMyContext()
          : base("name=MyConnectionString")
      {
          Database.SetInitializer&lt;EFMyContext&gt;(null);
      }
  
      public IDbSet&lt;Foo&gt; Foos
      {
          get
          {
              if (_foos == null)
              {
                  _foos = this.Set&lt;Foo&gt;();
              }
  
              return _foos;
          }
      }
  
      public IDbSet&lt;Bar&gt; Bars
      {
          get
          {
              if (_bars == null)
              {
                  _bars = this.Set&lt;Bar&gt;();
              }
  
              return _bars;
          }
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Configurations.Add(new FooConfiguration());
          modelBuilder.Configurations.Add(new BarConfiguration());
  
          base.OnModelCreating(modelBuilder);
      }
  </code></pre>
  
  <p>All fine so far. If I want to use this is an upper layer like this</p>
  
  <pre><code>public class MyService : IMyService
  {
      private IMyContext _context;
  
      public MyService(IMyContext context)
      {
          _context = context;
      }
  
      public void DoSomethingWithMyContext()
      {
          // fancy implementation code
      }
  }
  </code></pre>
  
  <p>Here I run into problems as I have a direct dependency on EntityFramework.dll as MyContext exposes properties of type IDBSet. This doesn't seem right and is very similar to <a href="https://stackoverflow.com/questions/7651237/entity-framework-code-first-wrapper-or-repository">this</a> question. </p>
  
  <p>So my question is, how to abstract away the direct dependency on EF? I thought about introducting my own repository and unit of work to pass around a live context but how would this impact change tracking and all the other neat EF features?</p>
  
  <p>The other thing I'm looking at is an IoC mechanism to manage the lifecycle of the context. Could I do away with the UoW at that point?</p>
  
  <p>Sorry for the vagueness, I'm at research saturation point with this and need a definitive place to start before implementation.</p>