使用 AutoMapper 对集合进行多态映射

Polymorphic Mapping of Collections with AutoMapper

TL;DR:我在使用多态映射时遇到了问题。我用一个测试套件制作了一个 github 回购协议来说明我的问题。请在这里找到它:LINK TO REPO

我正在努力实施 save/load 功能。为此,我需要确保我正在序列化的域模型以一种序列化友好的方式表示。为此,我创建了一组 DTO,其中包含进行有意义的保存或加载所需的最少信息集。

域的类似内容:

public interface IDomainType
{
  int Prop0 { get; set; }
}

public class DomainType1 : IDomainType
{
  public int Prop1 { get; set; }
  public int Prop0 { get; set; }
}

public class DomainType2 : IDomainType
{
  public int Prop2 { get; set; }
  public int Prop0 { get; set; }
}

public class DomainCollection
{
  public IEnumerable<IDomainType> Entries { get; set; }
}

...对于 DTO

public interface IDto
{
  int P0 { get; set; }
}

public class Dto1 : IDto
{
  public int P1 { get; set; }
  public int P0 { get; set; }
}

public class Dto2 : IDto
{
  public int P2 { get; set; }
  public int P0 { get; set; }
}

public class DtoCollection
{
  private readonly IList<IDto> entries = new List<IDto>();
  public IEnumerable<IDto> Entries => this.entries;
  public void Add(IDto entry) { this.entries.Add(entry); }
}

想法是 DomainCollection 表示应用程序的当前状态。目标是将 DomainCollection 映射到 DtoCollection 会生成一个 DtoCollection 实例,该实例包含 IDto 在映射到域时的适当实现。反之亦然。

这里有一个额外的小技巧,不同的具体域类型来自不同的插件程序集,所以我需要找到一种优雅的方式让 AutoMapper(或类似的,如果你知道更好的映射框架)做繁重的工作为我举重。

使用 structuremap,我已经能够从插件中找到并加载所有配置文件,并使用它们配置应用程序 IMapper。

我试过这样创建配置文件...

public class CollectionMappingProfile : Profile
{
  public CollectionMappingProfile()
  {
    this.CreateMap<IDomainType, IDto>().ForMember(m => m.P0, a => a.MapFrom(x => x.Prop0)).ReverseMap();

    this.CreateMap<DtoCollection, DomainCollection>().
       ForMember(fc => fc.Entries, opt => opt.Ignore()).
       AfterMap((tc, fc, ctx) => fc.Entries = tc.Entries.Select(e => ctx.Mapper.Map<IDomainType>(e)).ToArray());

    this.CreateMap<DomainCollection, DtoCollection>().
       AfterMap((fc, tc, ctx) =>
                {
                  foreach (var t in fc.Entries.Select(e => ctx.Mapper.Map<IDto>(e))) tc.Add(t);
                });
}

public class DomainProfile1 : Profile
{
  public DomainProfile1()
  {
    this.CreateMap<DomainType1, Dto1>().ForMember(m => m.P1, a => a.MapFrom(x => x.Prop1))
      .IncludeBase<IDomainType, IDto>().ReverseMap();
  }
}

public class DomainProfile2 : Profile
{
  public DomainProfile2()
  {
    this.CreateMap<DomainType2, IDto>().ConstructUsing(f => new Dto2()).As<Dto2>();

    this.CreateMap<DomainType2, Dto2>().ForMember(m => m.P2, a => a.MapFrom(x => x.Prop2))
      .IncludeBase<IDomainType, IDto>().ReverseMap();
  }
}

然后我编写了一个测试套件,以确保在将此功能与应用程序集成时映射将按预期运行。我发现每当 DTO 被映射到域(想想 Load)时,AutoMapper 会创建 IDomainType 的代理,而不是将它们解析到域。

我怀疑问题出在我的映射配置文件上,但我 运行 没有天赋。预先感谢您的意见。

Here's another link to the github repo

我花了一点时间重新组织回购协议。我什至模仿了一个核心项目和两个插件。这确保了当测试最终开始通过时我不会得到假阳性结果。

我发现解决方案有两个(大概)部分。

1) 我在滥用 AutoMapper 的 .ReverseMap() 配置方法。我假设它会执行我正在执行的任何自定义映射的倒数。不是这样!它只做简单的反转。很公平。关于它的一些 SO questions/answers: 1, 2

2) 我没有完全正确地定义映射继承。我来分解一下。

2.1) 我的域配置文件遵循以下模式:

public class DomainProfile1 : Profile
{
  public DomainProfile1()
  {
    this.CreateMap<DomainType1, IDto>().ConstructUsing(f => new Dto1()).As<Dto1>();
    this.CreateMap<DomainType1, Dto1>().ForMember(m => m.P1, a => a.MapFrom(x => x.Prop1))
      .IncludeBase<IDomainType, IDto>().ReverseMap();

    this.CreateMap<Dto1, IDomainType>().ConstructUsing(dto => new DomainType1()).As<DomainType1>();
  }
}

所以现在知道 .ReverseMap() 不是这里要使用的东西,很明显 Dto1 和 DomainType1 之间的映射定义不当。此外,DomainType1 和 IDto 之间的映射没有 link 回到基本的 IDomainType 到 IDto 映射。也是一个问题。最终结果:

public class DomainProfile1 : Profile
{
  public DomainProfile1()
  {
    this.CreateMap<DomainType1, IDto>().IncludeBase<IDomainType, IDto>().ConstructUsing(f => new Dto1()).As<Dto1>();
    this.CreateMap<DomainType1, Dto1>().IncludeBase<DomainType1, IDto>().ForMember(m => m.P1, a => a.MapFrom(x => x.Prop1));

    this.CreateMap<Dto1, IDomainType>().IncludeBase<IDto, IDomainType>().ConstructUsing(dto => new DomainType1()).As<DomainType1>();
    this.CreateMap<Dto1, DomainType1>().IncludeBase<Dto1, IDomainType>().ForMember(m => m.Prop1, a => a.MapFrom(x => x.P1));
  }
}

现在显式定义了映射的每个方向,并尊重继承。

2.2) IDomainType 和 IDto 的最基本映射位于配置文件内部,该配置文件还定义了 "collection" 类型的映射。这意味着,一旦我将项目拆分为模仿插件架构,仅测试最简单继承的测试就会以新的方式失败——无法找到基础映射。我所要做的就是将这些映射放入它们自己的配置文件中,并在测试中也使用该配置文件。太好了SRP.

在将自己的答案标记为已接受答案之前,我会将所学知识应用到我的实际项目中。希望我明白了,希望这对其他人有帮助。

有用links:

this

this one 是一个很好的重构练习。诚然,我将其用作构建示例的起点。所以,谢谢@Olivier。

我自己在研究多态映射问题时偶然发现了这个问题。答案很好,但如果你想从基础映射的角度来处理它并且有很多派生的 类,这只是另一种选择,你可以尝试以下方法:

CreateMap<VehicleEntity, VehicleDto>()
    .IncludeAllDerived();

CreateMap<CarEntity, CarDto>();
CreateMap<TrainEntity, TrainDto>();
CreateMap<BusEntity, BusDto>();

有关详细信息,请参阅 automapper docs