title: Unit Of Work & Generic Repository with Entity Framework 5
body: <p>I'm using ASP.NET MVC 4 with Entity Framework 5. I have model classes and Entity Maps to map existing tables to those model classes. All this is setup fine and works great.</p>
  
  <p>Now I want to mock this. I created Unit Of Work that takes the DataContext and uses a Generic Repository. Upon that I built services to be able to get data from many repositories at once and only needing to have one instance of the DataContext. This also works great.</p>
  
  <p><strong>Now to the problem:</strong> I want to test the services, with mock data. When I create the Unit Of Work instance, I want to be able to insert a DataContext that is mocked instead of the real DataContext.</p>
  
  <p>I tried to create a IContext interface and let the real and mocked DataContext implement that but ran into problems with DbSet. I tried to use IDbSet and creating a FakeDbSet but without success. I also read on the internet that mocking the context with IDbSet and using a FakeDbSet is a bad approach.</p>
  
  <p>Do you have any idea what would be the best way to achieve this? What I have now is the behavior I would like to keep, but would really like to be able to mock the data from the Model classes in the DataContext.</p>
  
  <p><em>I'm aware of that Entity Framework already comes with Unit Of Work behavior and that you don't need to add extra behavior on top of that. But I wanted to wrap that inside of another class that keeps track of all the repositories (called UnitOfWork class).</em></p>
  
  <p><strong>Edit:</strong> I wrote two articles explaining my solution with both LINQ and Entity Framework.</p>
  
  <p><a href="http://gaui.is/how-to-mock-the-datacontext-linq/">http://gaui.is/how-to-mock-the-datacontext-linq/</a></p>
  
  <p><a href="http://gaui.is/how-to-mock-the-datacontext-entity-framework/">http://gaui.is/how-to-mock-the-datacontext-entity-framework/</a></p>
  
  <p>Here's my code:</p>
  
  <p><strong>IRepository.cs</strong></p>
  
  <pre><code>public interface IRepository&lt;T&gt; where T : class
  {
      void Add(T entity);
      void Delete(T entity);
      void Update(T entity);
      T GetById(long Id);
      IEnumerable&lt;T&gt; All();
      IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
  }
  </code></pre>
  
  <p><strong>IUnitOfWork.cs</strong></p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      IRepository&lt;TEntity&gt; GetRepository&lt;TEntity&gt;() where TEntity : class;
      void Save();
  }
  </code></pre>
  
  <p><strong>Repository.cs</strong></p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
      private readonly IDbContext _context;
      private readonly IDbSet&lt;T&gt; _dbset;
  
      public Repository(IDbContext context)
      {
          _context = context;
          _dbset = context.Set&lt;T&gt;();
      }
  
      public virtual void Add(T entity)
      {
          _dbset.Add(entity);
      }
  
      public virtual void Delete(T entity)
      {
          var entry = _context.Entry(entity);
          entry.State = System.Data.EntityState.Deleted;
      }
  
      public virtual void Update(T entity)
      {
          var entry = _context.Entry(entity);
          _dbset.Attach(entity);
          entry.State = System.Data.EntityState.Modified;
      }
  
      public virtual T GetById(long id)
      {
          return _dbset.Find(id);
      }
  
      public virtual IEnumerable&lt;T&gt; All()
      {
          return _dbset;
      }
  
      public IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          return _dbset.Where(predicate);
      }
  }
  </code></pre>
  
  <p><strong>UnitOfWork.cs</strong></p>
  
  <pre><code>public class UnitOfWork&lt;TContext&gt; : IUnitOfWork where TContext : IDbContext, new()
  {
      private readonly IDbContext _ctx;
      private Dictionary&lt;Type, object&gt; _repositories;
      private bool _disposed;
  
      public UnitOfWork()
      {
          _ctx = new TContext();
          _repositories = new Dictionary&lt;Type, object&gt;();
          _disposed = false;
      }
  
      public IRepository&lt;TEntity&gt; GetRepository&lt;TEntity&gt;() where TEntity : class
      {
          if (_repositories.Keys.Contains(typeof(TEntity)))
              return _repositories[typeof(TEntity)] as IRepository&lt;TEntity&gt;;
  
          var repository = new Repository&lt;TEntity&gt;(_ctx);
          _repositories.Add(typeof(TEntity), repository);
          return repository;
      }
  
      public void Save()
      {
          _ctx.SaveChanges();
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this._disposed)
          {
              if (disposing)
              {
                  _ctx.Dispose();
              }
  
              this._disposed = true;
          }
      }
  }
  </code></pre>
  
  <p><strong>ExampleService.cs</strong></p>
  
  <pre><code>public class ExampleService
  {
      private IRepository&lt;Example&gt; m_repo;
  
      public ExampleService(IUnitOfWork uow)
      {
          m_repo = uow.GetRepository&lt;Example&gt;();
      }
  
      public void Add(Example Example)
      {
          m_repo.Add(Example);
      }
  
      public IEnumerable&lt;Example&gt; getAll()
      {
          return m_repo.All();
      }
  }
  </code></pre>
  
  <p><strong>ExampleController.cs</strong></p>
  
  <pre><code>public IEnumerable&lt;Example&gt; GetAll()
  {
      // Create Unit Of Work object
      IUnitOfWork uow = new UnitOfWork&lt;AppDataContext&gt;();
  
      // Create Service with Unit Of Work attached to the DataContext
      ExampleService service = new ExampleService(uow);
  
      return service.getAll();
  }
  </code></pre>