如何将数据实体映射到通用存储库中的业务实体和C#应用程序的工作单元

时间:2016-06-16 08:45:12

标签: c# wcf repository-pattern soa unit-of-work

我有C#应用程序的SOA层架构。我已在业务访问层'类库项目中定义了业务/域实体,......数据访问层'类库项目'中的数据实体。服务器端WCF的数据合同和WCF服务'类库项目'

我的问题是我应该如何以及在何处将数据实体映射到通用存储库和工作单元模式中的业务实体。

商业实体

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

数据实体

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

  }

}

会员资格背景

 using App.DAL.Entities;

 public class MembershipContext :BaseContext<MembershipContext>
{
    public DbSet<MemberEntity> Member { get; set; }
}

WCF数据合同

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

通用存储库

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

}

通用存储库实现

 public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    protected DbSet<TEntity> _DbSet;
    private readonly DbContext _dbContext;

    public GenericRepository()
    { }

    public GenericRepository(DbContext dbContext)
    {
        this._dbContext = dbContext;
        _DbSet = _dbContext.Set<TEntity>();
    }

    public IQueryable<TEntity> GetAll()
    {
        return _DbSet;
    }
   //...rest of code
 }

会员存储库

using App.Repository;
using App.DAL.Entities;

namespace App.Repository
{
public class Member_Repository: GenericRepository<MemberEntity>
{
    public Member_Repository() { }

    public Member_Repository(DbContext dbContext)
        :base(dbContext)
    { }
  }
}

工作单元

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

现在我的问题是当我从业务项目运行代码时,它应该只与存储库通信并仅使用业务实体作为成员但它要求我在业务项目中添加来自DAL的引用

这是我得到错误的代码

 using App.Repository.UnitOfWork;
 using App.Core.Entities;     

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

            // return entities.ToList();

            return null;
        }
    }

错误

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

enter image description here

1 个答案:

答案 0 :(得分:1)

现在,您正在使用DAL中的类型的BAL类中公开方法/属性。因此,要使用这些BAL类,您需要了解DAL。要解决此问题,您需要在将BAL对象从BAL返回之前将其转换为BAL对象。

更好的解决方案是让BAL不知道DAL对象。 DAL应该返回BAL可以处理的对象,并且还可以返回给调用者。从DAL类型到BAL类型的任何转换都是DAL的关注点,而不是BAL。