title: Repository pattern with EF6 + DatabaseFirst
body: <p><strong><em>Current System:</em></strong></p>
  
  <p>I am working on a project which is multi layered as shown below (in order of flow) and I am learning and trying to implement Repo Pattern with UOW on EF Database first.</p>
  
  <ul>
  <li>Service (Web API)</li>
  <li>Business (C# Class Library)</li>
  <li>Repository (Repo Pattern + UOW)</li>
  <li>ViewModels (Used by my Service for sent to my UI layer)</li>
  <li>Data (Entities)</li>
  <li>Database (SQL Server)</li>
  </ul>
  
  <p><strong><em>Repository:</em></strong></p>
  
  <p>Generic Repository:</p>
  
  <pre><code>public interface IRepository&lt;TEntity&gt; where TEntity : class
  {
      void Create(TEntity entity);
      IQueryable&lt;TEntity&gt; ReadAll();
      TEntity ReadById();
      IQueryable&lt;TEntity&gt; Filter(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
      TEntity ReadSingle(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
      TEntity ReadSingleOrDefault(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
      void Delete(TEntity entity);
  }
  </code></pre>
  
  <p>Repository Implementation:</p>
  
  <pre><code>internal class RepositoryBase&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class
  {
      private bool _isDisposed;
  
      private readonly DbSet&lt;TEntity&gt; _dbSet;
  
      private Entities _entities;
  
      public RepositoryBase(Entities entities)
      {
          this._entities = entities;
          this._dbSet = _entities.Set&lt;TEntity&gt;();
      }
  
      public IQueryable&lt;TEntity&gt; ReadAll()
      {
          return _dbSet;
      }
  
      public TEntity ReadById()
      {
          // Dummy return. Don't worry about returning null will be implemented later.
          return null;
      }
  
      public IQueryable&lt;TEntity&gt; Filter(System.Linq.Expressions.Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _dbSet.Where(predicate);
      }
  
      public TEntity ReadSingle(System.Linq.Expressions.Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _dbSet.Single(predicate);
      }
  
      public TEntity ReadSingleOrDefault(System.Linq.Expressions.Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _dbSet.SingleOrDefault(predicate);
      }
  
      public void Create(TEntity entity)
      {
          _dbSet.Add(entity);
      }
  
      public void Delete(TEntity entity)
      {
          _dbSet.Remove(entity);
      }
  
      public virtual void Dispose(bool isManuallyDisposing)
      {
          if (!_isDisposed)
          {
              if (isManuallyDisposing)
                  _tatwaEntities.Dispose();
          }
  
          _isDisposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      ~RepositoryBase()
      {
          Dispose(false);
      }
  </code></pre>
  
  <p>Generic UOW:</p>
  
  <pre><code>public interface IUnitOfWork
  {
      IRepository&lt;MyEntity&gt; MyEntityRepository { get; }
      //Other EntityRepositories
  
      void SaveChanges();
  }
  </code></pre>
  
  <p>UOW Implementation:</p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork, IDisposable
  {
      private Entities _entities;
  
      public UnitOfWork()
      {
          _entities = new entities();
      }
  
      private IRepository&lt;MyEntity&gt; _myEntityRepository;
      public IRepository&lt;MyEntity&gt; MyEntityRepository
      {
          get
          {
              return _myEntityRepository ?? (_myEntityRepository = new RepositoryBase&lt;MyEntity&gt;(_entities));
          }
      }
  
      public void SaveChanges()
      {
          _entities.SaveChanges();
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (disposing)
          {
              if (_entities != null)
              {
                  _entities.Dispose();
                  _entities = null;
              }
          }
      }
  }
  </code></pre>
  
  <p>Business Logic</p>
  
  <pre><code>public List&lt;MyViewModel&gt; ReadMyViewModelList()
      {
          var myVMList = new List&lt;MyViewModel&gt;();
          using (var unitOfWork = new UnitOfWork())
          {
              userList.AddRange(unitOfWork.MyEntityRepository.ReadAll().Select(myEntity =&gt; new myViewModel
              {
                  Prop1 = myEntity.Prop1,
                  Prop1 = myEntity.Prop2,
          etc...
              }));
          }
  
          return myVMList;
      }
  </code></pre>
  
  <p><strong><em>Problem:</em></strong></p>
  
  <ul>
  <li>My problem now is that, in my business code, when i use UOW to get
  data, I have to cast MyEntity to MyViewModel which I think is wrong.
  because the whole idea of using Repo Pattern with UOW is to abstract
  and avoid DB/Entity dependency on the business code and be able to
  independently test that layer.</li>
  <li>I am creating an instance of my DBEntities in both ReposirotyBase and
  UOW implemntation classes, which again I think is wrong and should
  only be created once and I am not sure where is the best place and
  way yo create that instance.</li>
  <li>Is my architecture model the correct approach? If not can someone
  please suggest the appropriate changes to it so as to make it more
  testable, maintainable and sound.</li>
  </ul>
  
  <p>Hope my question is elaborate. As I am in my learning phase of laying down architecture, I appreciate any additional suggestions/comments/improvements.</p>
  
  <p>Cheers.</p>