简体   繁体   English

自动将多个源映射到一个目标

[英]Automapper multiple source to one destination

I've been sifting through AutoMapper documentation and relevant questions to try and find a recommended solution to this but haven't been able to find it. 我一直在仔细阅读AutoMapper文档和相关问题,以尝试找到针对此问题的推荐解决方案,但未能找到它。

Let's say I have a source like the following: 假设我有一个类似以下的来源:

public class Person{
   public string PersonId{get;set;}
   public string FirstName {get;set;}
   public string LastName{get;set;}
   public List<Company> Companies {get;set;}
}

public class Company{
   public string CompanyName {get;set;}
   public string CompanyAddress {get;set;}
   public List<UserRoles> UserRoles
}

public class UserRoles{
   public string PersonId{get;set;}
   public List<Roles> Roles {get;set;}
}

public class Roles{
   public string RoleId{get;set;}
   public string RoleDescription {get;set;}
}

I would like to map the above source object to destination object as following: 我想将上述源对象映射到目标对象,如下所示:

public class Person{
   public string PersonId{get;set;}
   public string FirstName {get;set;}
   public string LastName{get;set;}
   public List<CompanyViewModel> CompanyViewModel {get;set;}
}

public class CompanyViewModel{
   public string CompanyName {get;set;}
   public string CompanyAddress {get;set;}
   public List<RolesViewModel> RolesViewModel {get;set;}
}

public class RolesViewModel{
   public string RoleId{get;set;}
   public string RoleDescription {get;set;}
}

I have tried following code to map together: 我试过下面的代码一起映射:

CreateMap<Person, PersonViewModel>();

CreateMap<Company, CompanyViewModel>();

CreateMap<UserRoles, IEnumerable<CompanyViewModel>>()
                   .ConvertUsing<CompanyRolesMapping>(); // Not sure

CreateMap<Roles, RolesViewModel>();

Parent level person data was able to mapping correctly, but the child level company and roles level data is not. 父级人员数据能够正确映射,而子级公司和角色级数据则不能。

I'm not sure about how to go about the third part (mapping that item to a item in a collection). 我不确定如何进行第三部分(将该项目映射到集合中的项目)。

What's the cleanest way of doing this? 最干净的方法是什么?

I have used following technique and it's works well. 我使用了以下技术,效果很好。

public void IntializeUserCompanyRolesMapping()
{
            CreateMap<Roles, RolesViewModel>();

            CreateMap<ICollection<Roles >, CompanyViewModel>()
                .ForMember(d => d.CompanyName , opt => opt.Ignore())
                .ForMember(d => d.CompanyAddress , opt => opt.Ignore())
                .ForMember(d => d.RolesViewModel, opt => opt.MapFrom(s => s));

            CreateMap<UserRoles, CompanyViewModel>()
                .ForMember(d => d.RolesViewModel , opt => opt.MapFrom(s => s.Roles ));

            CreateMap<Person, Person>()
                .ForMember(d => d.CompanyViewModel , opt => opt.MapFrom(s => MapCompanyToCompanyViewModel(s.Companies )));

}               

I have used custom method to loop over the list of companies and added into the list of company view model. 我使用自定义方法遍历公司列表,并将其添加到公司视图模型列表中。

    private Collection<CompanyViewModel> MapCompanyToCompanyViewModel(ICollection<Company> Companies )
    {
        var companyViewModels = new Collection<CompanyViewModel>();
                foreach (var company in Companies)
        {
            foreach (var companyViewModel in company.UserRoles.Select(Mapper.Map<CompanyViewModel>))
            {
                companyViewModels.Add(companyViewModel);
            }
        }
        return companyViewModels;

    }

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

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