Automapper 映射子项
Automapper mapping sub items
我正在尝试使用 Automapper 从前端对象层次结构映射到后端对象层次结构。这需要从源对象中的多个源动态创建子组件。我在其他地方没有遇到过这个问题。但是在这种情况下,新创建的对象需要自己的属性也被映射。
我已经添加了我在下面谈论的内容的通用版本。
config.CreateMap<BusinessObject, WebObject>()
.ForMember(d => d.Component, opts => opts.ResolveUsing(b =>
{
return new ComponentBusinessObject()
{
Date = b.Property1.Date,
Definition = b.Property2.Definition // This needs converting from (DefinitionWebObject to DefinitionBusinessObject)
};
}));
有谁知道在较低级别重新调用映射器的方法吗? (上例中的 'Definition'。)
基于 GTG 的评论:
如果在 BusinessObject
和 WebObject
映射之前将 DefinitionWebObject
和 DefinitionBusinessObject
映射到一起,您应该能够在父级中调用 Mapper.Map地图.
config.CreateMap<DefinitionWebObject, DefinitionBusinessObject>(); // Create sub-mapping first.
config.CreateMap<BusinessObject, WebObject>()
.ForMember(d => d.Component, opts => opts.ResolveUsing(b =>
{
return new ComponentBusinessObject()
{
Date = b.Property1.Date,
Definition = Mapper.Map<DefinitionBusinessObject>(b.Property2.Definition)
};
}));
我正在尝试使用 Automapper 从前端对象层次结构映射到后端对象层次结构。这需要从源对象中的多个源动态创建子组件。我在其他地方没有遇到过这个问题。但是在这种情况下,新创建的对象需要自己的属性也被映射。
我已经添加了我在下面谈论的内容的通用版本。
config.CreateMap<BusinessObject, WebObject>()
.ForMember(d => d.Component, opts => opts.ResolveUsing(b =>
{
return new ComponentBusinessObject()
{
Date = b.Property1.Date,
Definition = b.Property2.Definition // This needs converting from (DefinitionWebObject to DefinitionBusinessObject)
};
}));
有谁知道在较低级别重新调用映射器的方法吗? (上例中的 'Definition'。)
基于 GTG 的评论:
如果在 BusinessObject
和 WebObject
映射之前将 DefinitionWebObject
和 DefinitionBusinessObject
映射到一起,您应该能够在父级中调用 Mapper.Map地图.
config.CreateMap<DefinitionWebObject, DefinitionBusinessObject>(); // Create sub-mapping first.
config.CreateMap<BusinessObject, WebObject>()
.ForMember(d => d.Component, opts => opts.ResolveUsing(b =>
{
return new ComponentBusinessObject()
{
Date = b.Property1.Date,
Definition = Mapper.Map<DefinitionBusinessObject>(b.Property2.Definition)
};
}));