我已被平坦化的域对象插入的DTO如显示在下面的例子:使用AutoMapper压扁嵌套对象的更好方法?
public class Root
{
public string AParentProperty { get; set; }
public Nested TheNestedClass { get; set; }
}
public class Nested
{
public string ANestedProperty { get; set; }
}
public class Flattened
{
public string AParentProperty { get; set; }
public string ANestedProperty { get; set; }
}
// I put the equivalent of the following in a profile, configured at application start
// as suggested by others:
Mapper.CreateMap<Root, Flattened>()
.ForMember
(
dest => dest.ANestedProperty
, opt => opt.MapFrom(src => src.TheNestedClass.ANestedProperty)
);
// This is in my controller:
Flattened myFlattened = Mapper.Map<Root, Flattened>(myRoot);
我已经看过了一些实例,且到目前为止,这似乎是一个压平嵌套层级的方式。但是,如果子对象具有多个属性,则此方法不会节省太多编码。
我发现这个例子:
,但它需要映射的对象,由图()函数要求,按照我的理解,这将不与轮廓工作的实例。
我是AutoMapper的新手,所以我想知道是否有更好的方法来做到这一点。
我有和约翰一样的挑战。 – noocyte
没有人可以帮忙吗? –