使用AutoMapper将简单DTP转换为复杂DTO

问题描述 投票:1回答:3

我有一个看起来像这样的简单DTO:

    public partial class Company
    {
        public string NAME { get; set; }        

        public string CONTACT_ADDR1_1 { get; set; }
        public string CONTACT_ADDR2_1 { get; set; }
        public string CONTACT_CITY_1 { get; set; }
        public string CONTACT_STATE_1 { get; set; }
        public string CONTACT_ZIP_1 { get; set; }

        public string CONTACT_ADDR1_2 { get; set; }
        public string CONTACT_ADDR2_2 { get; set; }
        public string CONTACT_CITY_2 { get; set; }
        public string CONTACT_STATE_2 { get; set; }
        public string CONTACT_ZIP_2 { get; set; }

        public string CONTACT_ADDR1_3 { get; set; }
        public string CONTACT_ADDR2_3 { get; set; }
        public string CONTACT_CITY_3 { get; set; }
        public string CONTACT_STATE_3 { get; set; }
        public string CONTACT_ZIP_3 { get; set; }
     }

并且我想使用AutoMapper将其转换为具有类型列表的Company对象。我将有一个已知的地址数(6)。我只列出了三个。

    public partial class CompanyDto
    {
        public string NAME { get; set; }
        public List<AddressDto> { get; set; }
     }

    public partial class AddressDto
    {
        public string CONTACT_ADDR1 { get; set; }
        public string CONTACT_ADDR2 { get; set; }
        public string CONTACT_CITY { get; set; }
        public string CONTACT_STATE { get; set; }
        public string CONTACT_ZIP { get; set; }
     }

这是我遇到的困难:

cfg.CreateMap<Company, CompanyDto>();
c# automapper
3个回答
2
投票

我不建议为此使用automapper

花了我2分钟的时间进行输入,最多不会花10分钟的时间。我建议创建扩展方法并为复杂的映射转换您自己的DTO。通过第三方库进行配置将为您省去很多麻烦。

public static class CompanyExtensions{

   public static CompanyDto ToCompanyDto(this Company c){

     var cDto = new CompanyDto();

     cDto.Name = c.Name;
     cDto.Addresses = new List<AddressDto>();
     cDto.Addresses.Add(c.ToAddress1Dto());
     cDto.Addresses.Add(c.ToAddress2Dto());
     ...
     return cDto;


   }

  public static AddressDto ToCopmanyAddress1Dto(this Company c){
    return new AddressDto()
     {
      CONTACT_ADDR1 = c.CONTACT_ADDR1,
      CONTACT_ADDR2 = c.CONTACT_ADDR2,
      CONTACT_CITY = c.CONTACT_CITY,
      CONTACT_STATE = c.CONTACT_STATE,
      CONTACT_ZIP = c.CONTACT_ZIP 
     }
  }

 public static AddressDto ToCopmanyAddress2Dto(this Company c){
  ...
 }

 ... for the other addresses
}

0
投票
        CreateMap<Company, CompanyDto>()
            .AfterMap((s, d) =>
            {
                d.ADDRESSES = new System.Collections.Generic.List<AddressDto>();
                d.ADDRESSES.Add(new AddressDto
                {
                    CONTACT_ADDR1 = s.CONTACT_ADDR1_1,
                    CONTACT_ADDR2 = s.CONTACT_ADDR1_2,
                    CONTACT_CITY = s.CONTACT_CITY_1,
                    CONTACT_STATE = s.CONTACT_STATE_1,
                    CONTACT_ZIP = s.CONTACT_ZIP_1
                });
                d.ADDRESSES.Add(new AddressDto
                {
                    CONTACT_ADDR1 = s.CONTACT_ADDR2_1,
                    CONTACT_ADDR2 = s.CONTACT_ADDR2_2,
                    CONTACT_CITY = s.CONTACT_CITY_2,
                    CONTACT_STATE = s.CONTACT_STATE_2,
                    CONTACT_ZIP = s.CONTACT_ZIP_2
                });
            });

以上可能有效(并重复输入您的地址数)。我必须承认我实际上并没有运行此代码,但看起来可以通过


0
投票

我更希望@Train的答案,这是更容易理解的方法。

但是如果必须使用AutoMapper,则可以使用Custom Value Resolver,例如像:

public class CompanyToCompanyDtoResolver : IValueResolver<Company, CompanyDto, List<AddressDto>>
{            
    public List<AddressDto> Resolve(Company source, CompanyDto destination, List<AddressDto> destMember, ResolutionContext context)
    {
        var contacts = new List<AddressDto>();

        var companyType = typeof(Company);

        for (int i = 1; i <= 6; i++)
        {
            var address = new AddressDto();

            address.CONTACT_ADDR1 = (string)companyType
                .GetProperty(nameof(Company.CONTACT_ADDR1_1).Replace("_1", $"_{i}"))
                .GetValue(source);

            address.CONTACT_ADDR2 = (string)companyType
                .GetProperty(nameof(Company.CONTACT_ADDR2_1).Replace("_1", $"_{i}"))
                .GetValue(source);

            address.CONTACT_CITY = (string)companyType
                .GetProperty(nameof(Company.CONTACT_CITY_1).Replace("_1", $"_{i}"))
                .GetValue(source);

            address.CONTACT_STATE = (string)companyType
                .GetProperty(nameof(Company.CONTACT_STATE_1).Replace("_1", $"_{i}"))
                .GetValue(source);

            address.CONTACT_ZIP = (string)companyType
                .GetProperty(nameof(Company.CONTACT_ZIP_1).Replace("_1", $"_{i}"))
                .GetValue(source);

            contacts.Add(address);
        }

        return contacts;
    }
}

...

    var map = cfg.CreateMap<Company, CompanyDto>()
                 .ForMember(destination => destination.Contacts, 
                            source => source.MapFrom<CompanyToCompanyDtoResolver>());
© www.soinside.com 2019 - 2024. All rights reserved.