Automapper继承 - 再利用的地图 [英] Automapper inheritance -- reusing maps
本文介绍了Automapper继承 - 再利用的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用automapper创建一个单一的地图父对象,并重新使用此之间的孩子。
有关子属性我只想映射额外的字段。
这可能吗?我的代码看起来像这样
CreateCalculationMap(消息); ----这确实的BASE父mappping。
Mapper.CreateMap< QuoteMessage,CalculationGipMessage>() - 这是孩子。
&.INCLUDE LT; QuoteMessage,CalculationBase>() - 包括父?
.ForMember(A => a.OngoingCommission,B => b.MapFrom(C => c.OngoingASF)) - 儿童
.ForMember(A => a.SpecialRate,B => b.MapFrom(C => c.BlahBlah))); - 儿童
为什么它一直告诉我的属性不会被映射的父母呢?然而,我以为我列入他们CreateCalculationMap(消息);其中包含
Mapper.CreateMap< QuoteMessage,CalculationBase>()()
解决方案
FYI我想通了这一点。
公共静态IMappingExpression< A,T> ApplyBaseQuoteMapping< A,T>(这IMappingExpression< A,T> iMappingExpression)
,其中A:QuoteMessage
,其中T:CalculationGipMessage
{
iMappingExpression
.ForMember(一= GT; a.LoginUserName,b => b.MapFrom(C => c.LoginUserName))
.ForMember(A => a.AssetTestExempt,b => b.Ignore())
:
返回iMappingExpression;
}
Mapper.CreateMap< QuoteMessage,CalculationGipMessageChild>()
.ApplyBaseQuoteMappingToOldCol()
//做其他映射,这里
I am trying to use automapper to create a single map for a parent object and to reuse this amongst its children.
For child properties I only want to map the extra fields.
Is this possible? The code I have looks like this
CreateCalculationMap(message); ---- This does the BASE parent mappping.
Mapper.CreateMap<QuoteMessage, CalculationGipMessage>() -- This is the child.
.Include<QuoteMessage, CalculationBase>() -- Include the parent?
.ForMember(a => a.OngoingCommission, b => b.MapFrom(c => c.OngoingASF)) - Child
.ForMember(a => a.SpecialRate, b => b.MapFrom(c => c.BlahBlah))); - Child
Why does it keep telling me the parents properties are not mapped? Yet, I thought I included them in CreateCalculationMap(message); which contains
Mapper.CreateMap<QuoteMessage, CalculationBase>() ()
解决方案
FYI I figured this out
public static IMappingExpression<A, T> ApplyBaseQuoteMapping<A, T>(this IMappingExpression<A, T> iMappingExpression)
where A : QuoteMessage
where T : CalculationGipMessage
{
iMappingExpression
.ForMember(a => a.LoginUserName, b=> b.MapFrom(c => c.LoginUserName))
.ForMember(a => a.AssetTestExempt, b => b.Ignore())
;
return iMappingExpression;
}
Mapper.CreateMap<QuoteMessage, CalculationGipMessageChild>()
.ApplyBaseQuoteMappingToOldCol()
// do other mappings here
这篇关于Automapper继承 - 再利用的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文