title: Unit of work with EF 6 and Dependency injection Design problems
body: <p>I develop web application with entity framework 6, and have difficulties with designing the application structure. My main issue is how to deal with the dependency injection in my specific case.</p>
  
  <p>The code below is how I would like the application to look like. I'm using Autofac but I guess it is basic enough for every DI user to understand:</p>
  
  <pre><code>public interface IUnitOfWork
  {
      bool Commit();
  }
  
  public class UnitOfWork : IUnitOfWork, IDisposable
  {
      private DbContext _context;
  
      public UnitOfWork(DbContext context)
      {
          _context = context;
      }
  
      public bool Commit()
      {
          // ..
      }
  
      public bool Dispose()
      { 
            _context.Dispose();
      }
  }
  
  public class ProductsController : ApiController 
  {
       public ProductsController(IProductsManager managet)
  }   
  
  
  public class ProductsManager : IProductsManager
  {
      private Func&lt;Owned&lt;IUnitOfWork&gt;&gt; _uowFactory;
      private IProductsDataService _dataService;
  
      public Manager(Func&lt;Owned&lt;IUnitOfWork&gt;&gt; uowFactory, IProductsDataService dataService)
      {
          _dataService = dataService;
          _uowFactory = uowFactory;
      }
  
      public bool AddProduct(ProductEntity product)
      {
          using (ownedUow = _uowFactory())
          {
              var uow = ownedUow.Value;
  
              var addedProduct = _dataService.AddProduct(product);
  
              if (addedProduct != null)
                  uow.Commit();
          }
      }
  }
  
  public interface IProductsDataService
  {
      ProductEntity AddProduct (Product product)
  }
  
  public class ProductsDataService : IProductsDataService 
  {
      private IRepositoriesFactory _reposFactory;
  
      public DataService(IRepositoriesFactory reposFactory)
      {
          _reposFactory = reposFactory;
      }
  
      public ProductEntity AddProduct(ProductEntity product)
      {
          var repo = reposFactory.Get&lt;IProductsRepository&gt;();
  
          return repo.AddProduct(product);
      }
  }
  
  
  public interface IRepositoriesFactory
  {
      T Get&lt;T&gt;() where T : IRepository
  }
  
  public class RepositoriesFactory : IRepositoriesFactory
  {
      private ILifetimeScope _scope;
  
      public RepositoriesFactory(ILifetimeScope scope)
      {
          _scope = scope;
      }
  
      public T Get&lt;T&gt;() where T : IRepository
      {
          return _scope.Resolve&lt;T&gt;();
      }
  
  }
  
  public interface IProductsRepository
  {
      ProductEntity AddProduct(ProductEntity);
  }
  
  
  public ProductsRepository : IProductsRepository
  {
      private DbContext _context;
  
      public ProductsRepository(DbContext context)
      {
          _context = context;
      }
  
      public ProductEntity AddProduct(ProductEntity)
      {
          // Implementation..
      }
  }
  </code></pre>
  
  <p>This is the implementation I find ideal, however I don't know how to accomplish this, Because my ProductsDataService is singleton, therefore it is not related to the Owned scope that is created by the unit of works factory.
  Is there a way I can associate the Repositories to be created and take in their ctor the same DbContext that was created for the unit of work? Change the code in the RepositoriesFactory somehow?</p>
  
  <p>At the moment what I have is that the unit of work contains the repositories factory so that the context within the repositories will be the same as in the unit of work (I register the DbContext as per scope),
  The manager at the moment does the job of the DataService as well, which I dont like.</p>
  
  <p>I know I can pass around the UnitOfWork - method injection to the DataService methods, but I'd rather use Ctor injection, as it looks better in my opinion.</p>
  
  <p>What I want is to seperate this - A manager that it's job is to instantiate unit of works and commit them if needed, and another class (DataService) that actually executes the logic.</p>
  
  <p>Regardless, I would like to hear your opinion about this implementation if you have any comments / ideas for improvement.</p>
  
  <p>Thanks for your time!</p>
  
  <p>EDIT: This is what I ended up with:</p>
  
  <pre><code>public interface IUnitOfWork
  {
      bool Commit();
  }
  
  public class DatabaseUnitOfWork : IUnitOfWork
  {
      private DbContext _context;
  
      public DatabaseUnitOfWork(DbContext context)
      {
          _context = context;
      }
  
      public bool Commit()
      {
          // ..
      }
  }
  
  // Singleton
  public class ProductsManager : IProductsManager
  {
      private Func&lt;Owned&lt;IProductsDataService&gt;&gt; _uowFactory;
  
      public ProductsManager(Func&lt;Owned&lt;IProductsDataService&gt;&gt; uowFactory)
      {
          _uowFactory = uowFactory;
      }
  
      public bool AddProduct(ProductEntity product)
      {
          using (ownedUow = _uowFactory())
          {
              var dataService = ownedUow.Value;
  
              var addedProduct = _dataService.AddProduct(product);
  
              if (addedProduct != null)
                  uow.Commit();
          }
      }
  }
  
  public interface IProductsDataService : IUnitOfWork
  {
      ProductEntity AddProduct (Product product)
  }
  
  public class ProductsDataService : DatabaseUnitOfWork, IDataService 
  {
      private IRepositoriesFactory _reposFactory;
  
      public DataService(IRepositoriesFactory reposFactory)
      {
          _reposFactory = reposFactory;
      }
  
      public ProductEntity AddProduct(ProductEntity product)
      {
          var repo = _reposFactory .Get&lt;IProductsRepository&gt;();
  
          return repo.AddProduct(product);
      }
  }
  
  
  public interface IRepositoriesFactory
  {
      Get&lt;T&gt;() where T : IRepository
  }
  
  public class RepositoriesFactory : IRepositoriesFactory
  {
      private ILifetimeScope _scope;
  
      public RepositoriesFactory(ILifetimeScope scope)
      {
          _scope = scope;
      }
  
      public Get&lt;T&gt;() where T : IRepository
      {
          return _scope.Resolve&lt;T&gt;();
      }
  
  }
  
  public interface IProductsRepository
  {
      ProductEntity AddProduct(ProductEntity);
  }
  
  
  public ProductsRepository : IProductsRepository
  {
      private DbContext _context;
  
      public ProductsRepository(DbContext context)
      {
          _context = context;
      }
  
      public ProductEntity AddProduct(ProductEntity)
      {
          // Implementation..
      }
  }
  </code></pre>