title: How map data entities to business entities in generic repository and unit of work for C# application
body: <p>I have SOA Layer architecture for C# application. I have defined Business/ Domain Entities in Business Access Layer 'Class Library Project'.... Data Entities in Data Access Layer 'Class Library Project' and Data Contract for Server side WCF is under WCF Service 'Class Library Project'</p>
  
  <p>My Question is how and where I should map data entities to business entities in generic repository and unit of work pattern. </p>
  
  <h2>Business Entity</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace App.Core.Entities
  {
  public class Member
  {
      public int MemberID { get; set; }
  
      public string Title { get; set; }
  
      public string Surname { get; set; }
  
      public string Forename { get; set; }
  
      public string MiddleName { get; set; }
  }
  </code></pre>
  
  <h2>Data Entity</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;    
  
  namespace App.DAL.Entities
  {
  [Table("Member")]
  public class MemberEntity    
  {
      [Key]
      public int MemberID { get; set; }
  
      public string Title { get; set; }
  
      public string Surname { get; set; }
  
      public string Forename { get; set; }
  
      public string MiddleName { get; set; }
  
    }
  </code></pre>
  
  <p>}    </p>
  
  <h2>Membership Context</h2>
  
  <pre><code> using App.DAL.Entities;
  
   public class MembershipContext :BaseContext&lt;MembershipContext&gt;
  {
      public DbSet&lt;MemberEntity&gt; Member { get; set; }
  }
  </code></pre>
  
  <h2>WCF Data Contract</h2>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  using System.ServiceModel;
  using System.Runtime.Serialization;
  
  using App.Core.CoreInterfaces;
  
  namespace App.Services.Contracts 
  {
  [DataContract]
  public class MemberData : IIdentifiableEntity
  {
      [DataMember]
      public int MemberID { get; set; }
  
      [DataMember]
      public string Title { get; set; }
  
      [DataMember]
      public string Surname { get; set; }
  
      [DataMember]
      public string Forename { get; set; }
  
      [DataMember]
      public string MiddleName { get; set; }
  
      int IIdentifiableEntity.EntityId
      {
        get { return MemberID; }
        set { MemberID = value; }
      }
    }
  }
  </code></pre>
  
  <h2>generic repository</h2>
  
  <pre><code> public interface IGenericRepository&lt;TEntity&gt; where TEntity :class
  {
  
      global::System.Linq.IQueryable&lt;TEntity&gt; GetAll();
      TEntity GetEntityByID(int id);
      void InsertEntity(TEntity obj);
      void UpdateEntity(TEntity obj);
      void DeleteEntity(int id);
  
  }
  </code></pre>
  
  <h2>Generic Repository Implementation</h2>
  
  <pre><code> public class GenericRepository&lt;TEntity&gt; : IGenericRepository&lt;TEntity&gt; where TEntity : class
  {
      protected DbSet&lt;TEntity&gt; _DbSet;
      private readonly DbContext _dbContext;
  
      public GenericRepository()
      { }
  
      public GenericRepository(DbContext dbContext)
      {
          this._dbContext = dbContext;
          _DbSet = _dbContext.Set&lt;TEntity&gt;();
      }
  
      public IQueryable&lt;TEntity&gt; GetAll()
      {
          return _DbSet;
      }
     //...rest of code
   }
  </code></pre>
  
  <h2>Member Repository</h2>
  
  <pre><code>using App.Repository;
  using App.DAL.Entities;
  
  namespace App.Repository
  {
  public class Member_Repository: GenericRepository&lt;MemberEntity&gt;
  {
      public Member_Repository() { }
  
      public Member_Repository(DbContext dbContext)
          :base(dbContext)
      { }
    }
  }
  </code></pre>
  
  <h2>Unit of work</h2>
  
  <pre><code>using App.Repository;
  using App.DAL.DatabaseContext;
  using App.DAL.Entities;    
  
  namespace App.Repository.UnitOfWork
  {
   public class MembershipManagement_UOF:IDisposable
   {        
      protected Member_Repository _Member_Repository;  
  
     public Member_Repository Member_Repository
      {
          get
          {
              if (this._Member_Repository == null)
              {
                  this._Member_Repository = new Member_Repository(_MembershipContext);
              }
  
              return _Member_Repository;
          }
      }    
    }
  </code></pre>
  
  <p>Now my issue is when I run code from business project, it should only talk to repository and use only Business entity for Member but it asking me to add reference from DAL in business project</p>
  
  <p>here is code where I get Error</p>
  
  <pre><code> using App.Repository.UnitOfWork;
   using App.Core.Entities;     
  
   public IEnumerable&lt;Member&gt; GetAllMember()
      {
          using (var _uof = new MembershipManagement_UOF())
          {
              var entities = _uof.Member_Repository.GetAll();
  
              // return entities.ToList();
  
              return null;
          }
      }
  </code></pre>
  
  <h2>error</h2>
  
  <pre><code>Severity    Code    Description Project File    Line    Suppression State
   Error  CS0012  The type 'Member' is defined in an assembly that is not referenced. You must add a reference to assembly 'App.DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.  App.CoreServices    C:\My Work\Credit Union  Application\CreditSolutionApp\App.CoreServices\CoreServices\MembershipCore\MembershipCore.cs   23  Active
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/wuVMO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wuVMO.png" alt="enter image description here"></a></p>