简体   繁体   English

存储库模式和工作单元项目中的域实体和数据实体之间的c#映射

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

I have SOA Layer architecture for C# application. 我有用于C#应用程序的SOA层体系结构。 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' 我在业务访问层“类库项目”中定义了业务/域实体。...在数据访问层“类库项目”中的数据实体和服务器端WCF的数据合同在WCF服务“类库项目”下

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 WCF数据合同

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 现在我的问题是,当我从商业项目中运行代码,它应该只跟存储库并使用唯一的业务实体的成员,但它让我把DAL商业项目中添加引用

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). 从原始帖子中很难分辨出来,但是我怀疑您对IGenericRepository接口的具体实现是从DAL而不是从BLL(业务逻辑层)引用Member类。 The concrete repository needs to use the Member class from the BLL as its generic TEntity type. 具体的存储库需要使用BLL中的Member类作为其通用TEntity类型。 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. 具体存储库类的方法需要使用DAL Member类从数据库加载数据,然后将那些DAL Member实例映射到BLL Member实例,然后返回BLL Member实例。

Renaming the DAL Member class to something like MemberDto might help avoid confusion here. 将DAL成员类重命名为MemberDto之类的内容可能有助于避免混淆。 So, you might end up with something like (where IGenericRepository<TEntity> is in your BLL and MyMemberRepo is in your DAL): 因此,您可能最终会IGenericRepository<TEntity>类似的情况(其中IGenericRepository<TEntity>在BLL中,而MyMemberRepo在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...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用存储库和工作单元模式进行多个DB事务的C#实体框架 - C# Entity Framework Using the Repository and Unit of Work Pattern for Multiple DB Transactions 实体框架,存储库模式,工作单元和测试 - Entity Framework, Repository Pattern, Unit of Work and Testing 没有实体框架的 C# 中的存储库模式 - Repository pattern in C# without Entity Framework C#实体框架和存储库模式 - C# Entity Framework and Repository Pattern 带有 Entity Framework 6 异常数据读取器的 C# 存储库模式 SQL 查询与指定的实体不兼容 - C# repository pattern SQL query with Entity Framework 6 exception data reader is incompatible with specified entity 使用单例存储库和工作单元模式时,附加实体时出错 - Error in attaching entity when using singleton Repository and Unit of Work Pattern 使用Entity Framework 5和存储库模式和工作单元过滤内部集合 - Filtering inner collection with Entity Framework 5 and Repository pattern and Unit of Work 用于发布实体的 Asp.Net 核心存储库和工作单元模式 - Asp.Net core Repository and Unit Of Work Pattern for Posting an entity 使用Unity for Work of Unit / Repository模式创建Entity Framework对象 - Creating Entity Framework objects with Unity for Unit of Work/Repository pattern 实体框架+存储库+工作单元 - Entity Framework + Repository + Unit of Work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM