简体   繁体   中英

c# mapping between Domain Entity and Data Entity in Repository Pattern and unit of work project

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'

Business Entity

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; }
}

Data Entity

namespace App.DAL.Entities
{
[Table("Member")]
public class Member
{
    [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; }

  }

}

WCF Data Contract

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; }
    }
  }
}

generic repository

 public interface IGenericRepository<TEntity> where TEntity :class
{

    global::System.Linq.IQueryable<TEntity> GetAll();
    TEntity GetEntityByID(int id);
    void InsertEntity(TEntity obj);
    void UpdateEntity(TEntity obj);
    void DeleteEntity(int id);

}

Unit of work

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;
        }
    }    
  }

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

here is code where I get Error

 public IEnumerable<Member> GetAllMember()
    {
        using (var _uof = new MembershipManagement_UOF())
        {
            var entities = _uof.Member_Repository.GetAll();

            // return entities.ToList();

            return null;
        }
    }

error

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

It's a bit difficult to tell from the original post, but I suspect that your concrete implementation of the IGenericRepository interface is referencing the Member class from the DAL rather than from the BLL (Business Logic Layer). The concrete repository needs to use the Member class from the BLL as its generic TEntity type. The methods of the concrete repository class need to load the data from the DB using the DAL Member class, and then map those DAL Member instances to BLL Member instances, and then return the BLL Member instances.

Renaming the DAL Member class to something like MemberDto might help avoid confusion here. So, you might end up with something like (where IGenericRepository<TEntity> is in your BLL and MyMemberRepo is in your DAL):

public class MyMemberRepo : IGenericRepository<Member>
{
    public IEnumerable<Member> GetAllMember()
    {
        // 1. Load the data from the data store into an IEnumerable<MemberDto>.
        // 2. Map the IEnumerable<MemberDto> to an IEnumerable<Member>, perhaps
        //   using something like the open source AutoMapper project.
        // 3. Return the IEnumerable<Member>.
    }
    // ... other interface implementations...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM