简体   繁体   中英

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.

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;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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