简体   繁体   English

嵌套的集合在AutoMapper 5.1中不起作用

[英]Nested Collections Not Working in AutoMapper 5.1

Trying to upgrade to AutoMapper 5.1 from v4.2 and am finding that a collection isn't mapping at runtime - the source object has items in the collection, but the mapped destination property is empty. 试图从v4.2升级到AutoMapper 5.1,发现集合在运行时没有映射-源对象在集合中有项目,但是映射的目标属性为空。

Under 4.2, everything worked exactly as expected with the same mapping configuration (save for the MemberList.None in the CreateMap() ctor) 在4.2下,在相同的映射配置下,一切都按预期工作(在MemberMap中保存,但在CreateMap()ctor中没有)

I have DTOs like so 我有像这样的DTO

public class GeographicEntity
{
 ...
}

public class County : GeographicEntity
{
    ...
}

public class State : GeographicEntity
{
    public List<County> Counties { get; } = new List<County>();
}

And viewmodels like so 像这样的视图模型

public class GeographicEntityViewModel
{
  ...
}

public class CountyViewModel : GeographicEntityViewModel
{
  ...
}

public class StateViewModel : GeographicEntityViewModel
{
    public List<CountyViewModel> Counties { get; } = new List<CountyViewModel>();
}

And Mapping confirmation like so 并像这样确认映射

Mapper.Initialize(configuration =>
{
  configuration.CreateMap<GeographicEntity, GeographicEntityViewModel>(MemberList.None);

  configuration.CreateMap<County, CountyViewModel>(MemberList.None)
    .IncludeBase<GeographicEntity, GeographicEntityViewModel>();

  configuration.CreateMap<State, StateViewModel>(MemberList.None)
    .IncludeBase<GeographicEntity, GeographicEntityViewModel>();
});

After the Mapper.Map<> call, the Counties collection of the StateViewModel is empty (a list with 0 items) even though the source object has items in its .Counties collection: 调用Mapper.Map <>之后,即使源对象的.Counties集合中有项目,StateViewModel的Counties集合也为空(包含0项的列表):

var st = new State()
... (initialize the state, including the .Counties list)
var stateViewModel = Mapper.Map<StateViewModel>(st);

Any clues would be appreciated! 任何线索将不胜感激!

After some digging, it turns out that the AutoMapper 5 upgrade introduced some breaking changes. 经过一番挖掘,结果发现AutoMapper 5升级带来了一些重大更改。 Specifically, the behavior has changed in cases like mine where the destination collection has a getter but no setter. 特别是,在像我的情况下,行为发生了变化,例如我的目的地集合有吸气剂但没有二传手。 In AutoMapper 4, the default behavior was to use the destination property by default, rather than trying to create a new instance. 在AutoMapper 4中,默认行为是默认情况下使用destination属性,而不是尝试创建新实例。 AutoMapper 5 does NOT do that by default. 默认情况下,AutoMapper 5不会执行此操作。

The solution is to tell AutoMapper to use the destination value explicitly: 解决方案是告诉AutoMapper显式使用目标值:

.ForMember(dest => dest.Counties, o => o.UseDestinationValue())

I'm sure there's a good reason for introducing a breaking change like this, but it causes no end of heartache when you've implemented a broad pattern and now have to hunt down and fix every mapped object that might be affected by this change. 我敢肯定,引入这样的重大更改是有充分的理由的,但是当您实现了广泛的模式并且现在必须查找并修复可能受此更改影响的每个映射对象时,它不会引起任何痛心。

I'm almost tempted to just bail on the upgrade and stick with Automapper 4.2, as it did exactly what I needed it to without a lot of extra and unnecessary configuration required. 我几乎想保住升级并坚持使用Automapper 4.2,因为它完全满足了我的要求,而无需进行很多额外和不必要的配置。

For more detail, refer to https://github.com/AutoMapper/AutoMapper/issues/1599 有关更多详细信息,请参阅https://github.com/AutoMapper/AutoMapper/issues/1599

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM