title: Unit of Work and Repository Pattern
body: <p>I would like to implement simple IGenericRepository and IUnitOfWork interfaces in my application but I'm not sure whats the best way to do it. </p>
  
  <p>As far as I could understand, UnitOfWork should be used for writing, while Repository should be used for reading. I have encountered one architecture and I really like it, but I only found interfaces and not the implementations, and I'm not sure how should I implement those.</p>
  
  <pre><code>public interface IGenericRepository : IDisposable
  {
      IUnitOfWork CreateUnitOfWork();
      T FirstOrDefault&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; predicate) where T : class, IBaseEntity;
      IQueryable&lt;T&gt; Get&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; predicate = null, Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; sorter = null, params string[] includeProperties) where T : class, IBaseEntity;
      IQueryable&lt;T&gt; GetAll&lt;T&gt;() where T : class, IBaseEntity;
      IDbContext GetDbContext();
  }
  
  public interface IUnitOfWork : IDisposable
  {
      int Commit();
      bool Delete&lt;T&gt;(T entity) where T : class, IBaseEntity;
      int DeleteItems&lt;T&gt;(IList&lt;T&gt; entities) where T : class, IBaseEntity;
      bool Insert&lt;T&gt;(T entity) where T : class, IBaseEntity;
      int InsertItems&lt;T&gt;(IList&lt;T&gt; entities) where T : class, IBaseEntity;
      bool Update&lt;T&gt;(T entity) where T : class, IBaseEntity;
      int UpdateItems&lt;T&gt;(IList&lt;T&gt; entities) where T : class, IBaseEntity;
  }
  </code></pre>
  
  <p>I'm not sure how should those work. Should I use IDbContextFactory within repository to share DbContext between Repository and UnitOfWork or they should have separate DbContexts? If I implement UnitOfWork for write and Repository for read, should there be UnitOfWorks DbContext for write and Repositorys DbContext for read or they should share same DbContext?</p>
  
  <p>I would really appreciate good explanation of how DbContext and UnitOfWork/Repository should work.</p>
  
  <p>Those would be implemented in service in such way:</p>
  
  <pre><code>public CustomerService(IGenericRepository repository)
  {
      this.repository = repository;
      this.context = this.repository.GetDbContext();
  }
  
  public void UpdateCustomer(Customer customer)
  {
      var uow = this.repository.CreateUnitOfWork();
      uow.AddForSave(customer);
      uow.Commit();
  }
  
  public List&lt;Customer&gt; GetAll()
  {
      return this.repository.GetAll&lt;Customer&gt;();
  }
  </code></pre>
  
  <p>Any help, explanation about DbContext and UoW/Repository relation, or good tutorial similar to this implementation would help.</p>
  
  <p>Regards.</p>