简体   繁体   English

使用AutoMapper将复杂对象展平为多个展平对象

[英]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 CreateCompanyViewModelCreateUserTypeViewModel就像

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. 我希望将此RootVM展平为多个DTO。 The 3 DTO's for the above RootVM I have are like 我上面的RootVM的3个DTO就像

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. 注意:请注意,与UserDTO不同, CompanyDTOUserTypeDTO不是UserDTO嵌套对象(一部分)。

When I'm doing the mapping using AutoMapper RootVM properties gets mapped to UserDTO but CompanyDTO and UserTypeDTO are null as expected. 当我使用AutoMapper进行映射时, UserDTO属性被映射到UserDTO但是CompanyDTOUserTypeDTO都为空。

I tried mapping them by using ForMember function with MapFrom and ResolveUsing methods, but both of them shows error as 我尝试通过将ForMember函数与MapFromResolveUsing方法一起使用来映射它们,但是它们都显示错误为

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 我正在使用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/ . 更新-修复:我发现的是,我必须手动对所有属性使用.ForMember,否则要使自动约定生效,我需要使用https://github.com/AutoMapper/AutoMapper/wiki/Flatteninghttps://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 . 希望我能做到.ForMember(d => d, s => s.MapFrom(x => x.Company)) ,它将映射CreateCompanyViewModel => CompanyDTO所有属性。 This would have been very handy, but AutoMapper doesn't supports this. 这本来会很方便,但是AutoMapper不支持此功能。

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>();

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

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