[英]Mapping from Collection<T> to Collection<T> by convention
Is it possible to map from a property of type Collection<T> to another property of type Collection<T> by convention without the need to define the mapping explicitly?是否可以按照约定将 map 从 Collection<T> 类型的属性转换为 Collection<T> 类型的另一个属性,而无需显式定义映射?
class CollectionExample {
public static void Example() {
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Foo, FooDto>()
//.ForMember(dest => dest.Items, member => member.MapFrom(src => src.Items))
;
});
var mapper = config.CreateMapper();
var foo = new Foo() {
Items =
{
new Foo(),
new Foo(),
new Foo()
}
};
var fooDto = mapper.Map<Foo, FooDto>(foo);
Debug.Assert(fooDto.Items.Count == foo.Items.Count, $"There are only {fooDto.Items.Count} items in the dto object but we expected {foo.Items.Count} items.");
}
class Foo {
public Collection<Foo> Items { get; } = new Collection<Foo>();
}
class FooDto {
public Collection<FooDto> Items { get; } = new Collection<FooDto>();
}
}
When I uncomment the ForMember(..)
this works.当我取消注释
ForMember(..)
这有效。 Am I missing something for the convention based method?对于基于约定的方法,我是否遗漏了一些东西?
You need either a setter, or the MapFrom
, whichever you prefer:) That's the case in versions before 10. Check the link in the comment for the behavior in version 10.您需要一个 setter 或
MapFrom
,无论您喜欢哪个:) 10 之前的版本就是这种情况。检查评论中的链接以了解版本 10 中的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.