我想使用Automapper将我的模型对象映射到数据库对象。假设数据库对象超过30个字段,我希望从我的模型中的20个属性中的映射10。为了使它更复杂,我需要在更新记录时映射不同的属性,而不是在将新记录插入数据库时映射。
我正在使用的解决方案是创建2个通用类Insert和Update以及指定两个映射的映射配置文件。
以下示例:
public abstract class SyncMappingWrapper<TFrom> where TFrom : class
{
protected SyncMappingWrapper(TFrom model)
{
Model = model;
}
public TFrom Model { get; private set; }
}
public class Update<TFrom> : SyncMappingWrapper<TFrom> where TFrom : class
{
public Update(TFrom model)
: base(model)
{
}
}
public class Insert<TFrom> : SyncMappingWrapper<TFrom> where TFrom : class
{
public Insert(TFrom model)
: base(model)
{
}
}
然而,由于我需要为我没有映射的所有属性定义Ignore(),所以映射会变得讨厌,因为圈复杂度会进入天空(超过50):
CreateMap<Update<OracleModel>, LiveModel>()
.ForMember(des => des.ApprovedBy, opt => opt.Ignore())
.ForMember(des => des.ApprovedDate, opt => opt.Ignore())
...
.ForMember(des => des.UNSPSC, opt => opt.Ignore())
.ForMember(des => des.BaseUnit, opt => opt.MapFrom(src => src.Model.UOM.BaseUOM.PerSalesUnit))
.ForMember(des => des.BaseUOM, opt => opt.MapFrom(src => src.Model.UOM.BaseUOM.UnitOfMeasure.Code))
.ForMember(des => des.SalesUnit, opt => opt.MapFrom(src => src.Model.UOM.SalesUOM.PerSalesUnit))
.ForMember(des => des.SalesUOM, opt => opt.MapFrom(src => src.Model.UOM.SalesUOM.UnitOfMeasure.Code))
.ForMember(des => des.OrderUnit, opt => opt.MapFrom(src => src.Model.UOM.OrderUOM.PerSalesUnit))
.ForMember(des => des.OrderUOM, opt => opt.MapFrom(src => src.Model.UOM.OrderUOM.UnitOfMeasure.Code))
.ForMember(des => des.SalesPrice, opt => opt.MapFrom(src => src.Model.Price.Value))
.ForMember(des => des.Alternate, opt => opt.Ignore())
.ForMember(des => des.ManufacturerID, opt => opt.Ignore())
.ForMember(des => des.ProductCode, opt => opt.MapFrom(src => src.Model.ProductCode))
.ForMember(des => des.ProductName, opt => opt.MapFrom(src => src.Model.ProductName))
.ForMember(des => des.ProductHTML, opt => opt.Ignore())
.ForMember(des => des.Version, opt => opt.Ignore())
...
.ForMember(des => des.UnitsOfMeasure2, opt => opt.Ignore())
.ForMember(des => des.Manufacturer, opt => opt.Ignore());
我通过创建新对象解决了插入新记录的问题:
CreateMap<Insert<OracleModel>, LiveModel>()
.ConstructUsing(x => new LiveModel
{
BaseUnit = x.Model.UOM.BaseUOM.PerSalesUnit,
BaseUOM = x.Model.UOM.BaseUOM.UnitOfMeasure.Code,
SalesUnit = x.Model.UOM.SalesUOM.PerSalesUnit,
SalesUOM = x.Model.UOM.SalesUOM.UnitOfMeasure.Code,
OrderUnit = x.Model.UOM.OrderUOM.PerSalesUnit,
OrderUOM = x.Model.UOM.OrderUOM.UnitOfMeasure.Code,
SalesPrice = x.Model.Price.Value,
LeadTime = x.Model.LeadTime,
ProductCode = x.Model.ProductCode,
ProductName = x.Model.ProductName,
SupplierCode = x.Model.SupplierCode,
Weight = x.Model.Weight
})
.ForAllMembers(xc => xc.Ignore());
但是对于我想将属性映射到现有对象而不是新实例的更新它不起作用:
Mapper.Map(update, existingRecord);
我宁愿避免使用DynamicMap()来完全控制映射(因此我不会错误地映射随机属性)。我的目标是解决圈复杂度问题。请不要建议ValueInjecter或任何其他方法。我正在寻找AutoMapper中的解决方案。
答案 0 :(得分:1)
五年后,但是这里有一个减少这种映射复杂性问题的建议。您可以创建一个扩展程序,以帮助您首先忽略所有成员:
public static class AutoMapperExtension {
public static IMappingExpression<TSource, TDest> IgnoreAllMembers<TSource, TDest>(this IMappingExpression<TSource, TDest> expression) {
expression.ForAllMembers(opt => opt.Ignore());
return expression;
}
}
然后使用它来定义您想要执行的映射:
Mapper.CreateMap<Insert<OracleModel>, LiveModel>()
.IgnoreAllMembers()
.ForMember(d => d.BaseUnit, o => o.MapFrom(s => s.Model.UOM.BaseUOM.PerSalesUnit))
/* Mapping for other members here. */;