从表示层映射到后端的通用方式

时间:2015-07-17 21:51:50

标签: c# entity-framework generics layered

我有一个带有分层层(webforms)的Web应用程序(presentation-BLL(service)-DAL和带有实体框架的存储库,并以通用方式执行。

系统功能非常好,但问题是我将域对象直接暴露到表示层。我想在服务层中做switchover/mapping,但我无法弄清楚如何以通用的方式做到这一点。

在我的表示层中,我有以下呼叫:

var language = repository.GetByID<Language>(Guid.Parse("18022719-faa0-447a-b054-3e1ae6dd8c67"));

在我的服务层中,该方法如下:

/// <summary>
    /// Get instance from database by ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ID"></param>
    /// <returns></returns>
    public T GetByID<T>(Guid ID) where T : class
    {
        return iow.GetRepository<T>().GetByID(ID);
    }

这一切都包含在unitofwork(低)中。

我头疼的是表示层引用了一个类似于实体的模型,它基本上是扁平的域对象。 Servicelayer也引用了这个模型,当然还有用于与数据库通信的域模型。对于表示层中的语言实体,全名为Model.Language,而在服务层中,它将为DAL.Language(向后传递)。

我无法弄清楚如何将类型更改为下一个调用,通常是:

return iow.GetRepository<T>().GetByID(ID);

有任何建议......?

1 个答案:

答案 0 :(得分:1)

在某些时候,您需要在您的架构中引入领域知识,或者您需要使用约定和反射。以下是实现目标的三种方法。

选项1:

由于表示模型是一个更高阶的概念,其消费者最终依赖于较低的实体层,因此您可以尝试通过隐式运算符将表示模型单向耦合到实体模型,您必须手动实现(或代码生成)。以下面的例子为例:

namespace Model
{
    public class Language
    {
        public static implicit operator Model.Language(DAL.Language language)
        {
            return new Model.Language()
            {
                /* map values here */
            }
        }
        public static implicit operator DAL.Language(Model.Language language)
        {
            return new DAL.Language()
            {
                /* map values here */
            }
        }
    }
}

通过这种方式,您可以将一个模型替换为另一个模型,并让隐式运算符在它们之间进行转换。

选项2:

指定要实例化以处理映射的映射器类。尝试以下方法将两个模型分离,但仍需要编写或生成特定于域的代码:

public  class   Presentation<TPresentationModel, TDALModel, TMapper>
        where   TMapper : Presentation<TPresentationModel, TDALModel, TMapper>.BaseMapper
{

    public  abstract    class   BaseMapper
    {

        public  abstract    TDALModel           ConvertPresentationModelToDALModel(TPresentationModel presentationModel);
        public  abstract    TPresentationModel  ConvertDALModelToPresentationModel(TDALModel dalModel);

    }

}

选项3:

在模型之间使用反射和具有相同名称的属性约定。像这样:

public  class   Entity
                <
                    TEntity,
                    TDataObject,
                    TIBusiness,
                    TPrimaryKey
                >
        where   TEntity     : Entity<TEntity, TDataObject, TIBusiness, TPrimaryKey>
        where   TDataObject : Entity<TEntity, TDataObject, TIBusiness, TPrimaryKey>.BaseDataObject, new()
        where   TIBusiness  : Entity<TEntity, TDataObject, TIBusiness, TPrimaryKey>.IBaseBusiness
{

    public
    abstract    class       BaseDataObject
    {
        public  TPrimaryKey Id;
    }

    public      interface   IBaseBusiness
    {
        TDataObject GetByID(TPrimaryKey id);
    }

    public  class   PresentationMapper<TPresentationModel>
            where   TPresentationModel  : new()
    {

        private
        static  Dictionary
                <
                    string, 
                    MemberInfo
                >                   entityFieldsAndProperties       = typeof(TDataObject).GetProperties().AsEnumerable<MemberInfo>().Union(typeof(TDataObject).GetFields().AsEnumerable<MemberInfo>()).ToDictionary<MemberInfo, string>(memberInfo=>memberInfo.Name);

        private
        static  Dictionary
                <
                    string, 
                    MemberInfo
                >                   presentationFieldsAndProperties = typeof(TPresentationModel).GetProperties().AsEnumerable<MemberInfo>().Union(typeof(TPresentationModel).GetFields().AsEnumerable<MemberInfo>()).ToDictionary<MemberInfo, string>(memberInfo=>memberInfo.Name);

        private TIBusiness          business;

        public                      PresentationMapper(TIBusiness business) { this.business = business; }

        public  TPresentationModel  GetByID(TPrimaryKey id)
        {
            var dataObject  = this.business.GetByID(id);
            return this.map(dataObject);
        }

        private TPresentationModel  map(TDataObject dataObject)
        {
            var returnModel             = new TPresentationModel();
            var presentationMemberInfo  = (MemberInfo) null;

            foreach(string key in entityFieldsAndProperties.Keys)
            if (presentationFieldsAndProperties.TryGetValue(key, out presentationMemberInfo))
            {
                this.copy
                (
                    value:  this.getValue(from: dataObject, @using: entityFieldsAndProperties[key]), 
                    to:     returnModel, 
                    @using: presentationMemberInfo
                );
            }
            return returnModel;
        }

        private object              getValue(TDataObject from, MemberInfo @using)
        {
            return @using is PropertyInfo ? ((PropertyInfo) @using).GetValue(from) : ((FieldInfo) @using).GetValue(from);
        }

        private void                copy(object value, TPresentationModel to, MemberInfo @using)
        {
            if (@using is PropertyInfo)     ((PropertyInfo) @using).SetValue(to, value);
            else                            ((FieldInfo) @using).SetValue(to, value);
        }

    }

}

简单地使用适当的业务类实现实例化PresentationMapper并在presentationMapper上调用GetById。调整班级名称并调整您的需求。