在某些时候,您需要将领域知识引入到您的架构中,或者您需要使用约定和反射。这里有三种方法来完成你正在尝试做的事情。
选项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。调整类名并根据您的需要进行调整。