【问题标题】:AutoMapper - Map source object to nested objectAutoMapper - 将源对象映射到嵌套对象
【发布时间】:2021-09-22 21:59:59
【问题描述】:

如何将单个对象映射到嵌套对象

下面是我的实体。

class Employee
{
    public string name { get; set; }
    public string city_name { get; set; }
    public string State_name { get; set; }
}

我想映射到

class Employee
{
    public string Name;
    public Location Location;
}

class Location
{
    public string City;

    public string State;
}

请注意,属性名称不同。任何人都可以帮助使用 AutoMapper 映射这些

【问题讨论】:

  • 您应该只检查automapper documentation 并创建自定义映射。看起来自动映射器文档示例与您的场景非常相似

标签: c# asp.net asp.net-mvc automapper


【解决方案1】:

解决方案 1:ForPath

1.1 创建Profile 实例,继承自Profile,并将配置放入构造函数中。

1.1.1 使用ForPath 映射嵌套属性。

public class EmployeeProfile : Profile
{
    public EmployeeProfile()
    {
        CreateMap<Employee, EmployeeDto>()
            .ForPath(dest => dest.Location.City, opt => opt.MapFrom(src => src.city_name))
            .ForPath(dest => dest.Location.State, opt => opt.MapFrom(src => src.State_name));
    }
}

1.2 将配置文件添加到映射器配置

public static void Main()
{
    var config = new MapperConfiguration(cfg =>
    {

        cfg.AddProfile<EmployeeProfile>();

        // Note: Demo program to use this configuration rather with EmployeeProfile
        /*
        cfg.CreateMap<Employee, EmployeeDto>()
            .ForPath(dest => dest.Location.City, opt => opt.MapFrom(src => src.city_name))
            .ForPath(dest => dest.Location.State, opt => opt.MapFrom(src => src.State_name));
        */      
    });
    IMapper mapper = config.CreateMapper();
        
    var employee = new Employee{name = "Mark", city_name = "City A", State_name = "State A"};

    var employeeDto = mapper.Map<Employee, EmployeeDto>(employee);
}

Sample Solution 1 on .Net Fiddle


解决方案 2:AfterMap

2.1 创建Profile 实例,继承自Profile 并将配置放入构造函数中。

2.1.1在map发生后使用AfterMap进行映射嵌套属性。

public class EmployeeProfile : Profile
{
    public EmployeeProfile()
    {
        CreateMap<Employee, EmployeeDto>()
            .AfterMap((src, dest) => { dest.Location = 
                    new Location {
                        City = src.city_name,
                        State = src.State_name
                    };
                });
    }
}

2.2 将配置文件添加到映射器配置

public static void Main()
{
    var config = new MapperConfiguration(cfg =>
    {

        cfg.AddProfile<EmployeeProfile>();

        // Note: Demo program to use this configuration rather with EmployeeProfile
        /*
        cfg.CreateMap<Employee, EmployeeDto>()
            .AfterMap((src, dest) => { dest.Location = 
                    new Location {
                        City = src.city_name,
                        State = src.State_name
                    };
                });
        */      
    });
    IMapper mapper = config.CreateMapper();
        
    var employee = new Employee{name = "Mark", city_name = "City A", State_name = "State A"};

    var employeeDto = mapper.Map<Employee, EmployeeDto>(employee);
}

Sample Solution 2 on .Net Fiddle


参考文献

  1. ForPath
  2. Profile Instances
  3. Before and After Map

【讨论】:

    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2012-02-25
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2020-04-03
    • 2017-06-11
    相关资源
    最近更新 更多