我正在将一个非常深层嵌套的实体映射到一个扁平化的Dto对象,并想知道我如何使用AutoMapper优雅地处理它.我知道我可以对每个属性进行null检查,因为它正在被映射,但这对于以下内容非常难看:
ForMember(s => s.Property, o => o.MapFrom(s => s.Parent.Child1.Child2.Child3.Property)
Run Code Online (Sandbox Code Playgroud)
所以我猜我可以使用各种地图配置到相同的目标对象......但是相对不熟练使用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));
Run Code Online (Sandbox Code Playgroud)
我认为AutoMapper实际上会null自动处理这样的传播.你的例子:
ForMember(s => s.Property, o => o.MapFrom(s => s.Parent.Child1.Child2.Child3.Property)
Run Code Online (Sandbox Code Playgroud)
应该解决null(我认为),如果有任何中间成员null.
示例: https ://dotnetfiddle.net/hMo3wa
| 归档时间: |
|
| 查看次数: |
1015 次 |
| 最近记录: |