title: Dependency injection with Unit of Work pattern
body: <p>I'm trying to implement unit of work pattern for my repositories in C#/.NET. My plan is to give UoW as a parameter to repositories. Here's an example how it could be used:</p>
  <pre><code>using (var uow = new UnitOfWork()) 
  {
      var itemRepository = new ItemRepository(uow);
      itemRepository.Add(new Item());
      uow.Commit();
  }
  </code></pre>
  <p>Also, for simple operations (when transactions are not needed) repositories should be able to be used without Unit of Work:</p>
  <pre><code>var itemRepository = new ItemRepository();
  var item = itemRepository.Get(itemId);
  </code></pre>
  <p>UnitOfWork/Repository could get a database connection from ConnectionFactory. Connection factory receives connection options via dependency injection. However, here's my problem: <strong>How do repositories get reference to ConnectionFactory instance?</strong> Repositories are created manually, so they can't have dependencies injected via constructor. One option would be to have repository factories, which could have their dependencies injected. In that case usage could be like this:</p>
  <pre><code>using (var uow = new UnitOfWork()) 
  {
      var itemRepository = itemRepositoryFactory.Create(uow);
      itemRepository.Add(new Item());
      uow.Commit();
  }
  </code></pre>
  <p>The drawback in that solution is that each repository will need its own factory, and there will be quite many. <strong>Are there any other solutions to circumvent this problem?</strong></p>