简体   繁体   中英

Flatten Complex Object To Multiple Flatten Objects Using AutoMapper

I have a View Model such as

public class RootViewModel
{
    public CreateCompanyViewModel Company { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }

    public string Password { get; set; }

    public string ConfirmPassword { get; set; }

    public CreateUserTypeViewModel UserType { get; set; }
}

And CreateCompanyViewModel and CreateUserTypeViewModel are like

public class CreateCompanyViewModel
{
    public string CompanyName { get; set; }
}

public class CreateUserTypeViewModel
{
    public string UserTypeName { get; set; }
}

I want this RootVM to be flattened to multiple DTO's. The 3 DTO's for the above RootVM I have are like

public class UserDTO
{
    public string Name { get; set; }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }

    public string Password { get; set; }

    public string ConfirmPassword { get; set; }
}

public class CompanyDTO
{
    public string CompanyName { get; set; }
}

public class UserTypeDTO
{
    public string UserTypeName { get; set; }
}

NOTE : Note that CompanyDTO and UserTypeDTO are not nested object (part of) UserDTO unlike RootVM.

When I'm doing the mapping using AutoMapper RootVM properties gets mapped to UserDTO but CompanyDTO and UserTypeDTO are null as expected.

I tried mapping them by using ForMember function with MapFrom and ResolveUsing methods, but both of them shows error as

Custom configuration for members is only supported for top-level individual members on a type.

UPDATE Below is my mapping code

CreateMap<RootViewModel, CompanyDTO>();
CreateMap<RootViewModel, UserDTO>();
CreateMap<RootViewModel, UserTypeDTO>();
CreateMap<CreateCompanyViewModel, CompanyDTO>();
CreateMap<CreateUserTypeViewModel, UserTypeDTO>();

I'm using AutoMapper 5.2.0

UPDATE - Fix : Well what I found is, either I have to use .ForMember for all the properties manually, else for automatic convention to work, I need to use https://github.com/AutoMapper/AutoMapper/wiki/Flattening or https://arnabroychowdhurypersonal.wordpress.com/2014/03/08/flattening-object-with-automapper/ .

This is the only way to make it work.

Wish I could do .ForMember(d => d, s => s.MapFrom(x => x.Company)) and it'd map all the properties from CreateCompanyViewModel => CompanyDTO . This would have been very handy, but AutoMapper doesn't supports this.

Try following

        CreateMap<CreateCompanyViewModel, CompanyDTO>();
        CreateMap<CreateUserTypeViewModel, UserTypeDTO>();

        CreateMap<RootViewModel, CompanyDTO>()
            .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Company.CompanyName));

        CreateMap < RootViewModel, UserTypeDTO()
              .ForMember(dest => dest.UserTypeName, opt => opt.MapFrom(src => src.UserType.UserTypeName));

        CreateMap<RootViewModel, UserDTO>();

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