自动映射嵌套映射

rya*_*yan 8 c# automapper

我已经阅读了嵌套映射维基页面,但似乎不喜欢多层嵌套.我已经创建了以下地图并定义了类.

AutoMapper.Mapper.CreateMap<Address, AddressDTO>();
AutoMapper.Mapper.CreateMap<MatchCompanyRequest, MatchCompanyRequestDTO>();

public class MatchCompanyRequest
{
    Address Address {get;set;}
}

public class MatchCompanyRequestDTO
{
    public CompanyInformationDTO {get;set;}
}

public class CompanyInformationDTO {get;set;}
{
    public string CompanyName {get;set;}
    public AddressDTO Address {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

但是以下代码......

// works
matchCompanyRequestDTO.companyInformationDTO.Address =
    AutoMapper.Mapper.Map<Address, AddressDTO>(matchCompanyRequest.Address);

// fails
matchCompanyRequestDTO =
    AutoMapper.Mapper
        .Map<MatchCompanyRequest, MatchCompanyRequestDTO>(matchCompanyRequest);
Run Code Online (Sandbox Code Playgroud)

这种深度嵌套是否有效,而且我的配置不正确?或者这种嵌套还没有得到支持?

- 编辑

对于任何有兴趣的人,我都无法控制DTO.

Bar*_*osz 6

它缺少Address到Address的映射CompanyInformationDTO,因为这些对象在同一个nest-level上.

地图是为MatchCompanyRequest- > 创建的MatchCompanyRequestDTO,但无法确定它是否可以映射AddressCompanyInformationDTO.

所以你MatchCompanyRequestDTO实际上可以和你的声明一样CompanyInformationDTO:

public class MatchCompanyRequestDTO
{
    public string CompanyName {get;set;}
    public AddressDTO Address {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

如果您想使用自动映射,这当然只会影响您.您仍然可以手动配置您的地图,但似乎应该修复DTO,让我们尝试:

public class CustomResolver : ValueResolver<Address, CompanyInformationDTO>
{
    protected override CompanyInformationDTO ResolveCore(Address source)
    {
        return new CompanyInformationDTO() { Address = Mapper.Map<Address, AddressDTO>(source) };
    }
}
// ...

AutoMapper.Mapper.CreateMap<MatchCompanyRequest, MatchCompanyRequestDTO>()
    .ForMember(dest => dest.companyInformationDTO, opt => opt.ResolveUsing<CustomResolver>().FromMember(src => src.Address)); // here we are telling to use our custom resolver that converts Address into CompanyInformationDTO
Run Code Online (Sandbox Code Playgroud)


小智 5

重要的是要定义导航的深度,以预知stackoverflow问题。想象一下这种可能性:

在NxN模型中有2个实体“ 用户”和“ 通知”(并且您有DTO对象来表示),当您在映射器表达式中未设置MaxDepth的情况下使用自动映射器时,“休斯顿,我们遇到了问题” :)。

下面的代码显示了解决所有Mapper问题的解决方法。如果需要,可以为每个映射器定义。喜欢这个问题

解决方案1(全局定义)

public class AutoMapperConfig
{
    public static void RegisterMappings()
    {
        Mapper.Initialize(mapperConfiguration =>
        {
            mapperConfiguration.AddProfile<DomainModelToYourDTOsMappingProfile>();
            mapperConfiguration.AddProfile<YourDTOsToDomainModelMappingProfile>();
            mapperConfiguration.AllowNullCollections = true;
            mapperConfiguration.ForAllMaps(
                (mapType, mapperExpression) => {
                    mapperExpression.MaxDepth(1);
                });
        }
    }
Run Code Online (Sandbox Code Playgroud)

解决方案2(对于每个映射器)

 public class AutoMapperConfig
 {
     public static void RegisterMappings()
     {
         Mapper.CreateMap<User, DTOsModel>()
               .MaxDepth(1);
     }
 }
Run Code Online (Sandbox Code Playgroud)

  • 这不是OP的正确答案,但确实是很好的信息。谢谢! (2认同)