【发布时间】:2016-11-02 21:53:54
【问题描述】:
我正在将一个嵌套非常深的实体映射到一个扁平的Dto 对象,并且想知道如何使用 AutoMapper 优雅地处理这个问题。我知道我可以在映射每个属性时对其进行空值检查,但这对于以下内容会变得非常难看:
ForMember(s => s.Property, o => o.MapFrom(s => s.Parent.Child1.Child2.Child3.Property)
所以我想我可以对同一个目标对象使用各种地图配置...但是相对不熟练使用 AutoMapper 我不确定这对性能有什么影响。我还有什么其他更好的方法来实现我想要的?
为了重新迭代,我希望避免这样的事情(我知道下面的代码可能无法编译......这只是一个例子),我有为每个成员做:
ForMember(s => s.Property, o => o.MapFrom(
s => s.Parent == null ? string.Empty :
s => s.Parent.Child1 == null ? string.Empty :
s => s.Parent.Child1.Child2 == null ? string.Empty :
s => s.Parent.Child1.Child2.Child3 == null ? string.Empty :
s => s.Parent.Child1.Child2.Child3.Property));
【问题讨论】:
标签: automapper