2

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

系统功能非常好,但问题是我将域对象直接暴露给表示层。我想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(iow)中。

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

我无法弄清楚如何将类型更改为下一个调用,一般来说:

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

有什么建议么...?

4

1 回答 1

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。调整类名并根据您的需要进行调整。

于 2015-07-23T23:29:52.020 回答