Automapper 映射嵌套对象

Posted

技术标签:

【中文标题】Automapper 映射嵌套对象【英文标题】:Automapper mapping nested objects 【发布时间】:2017-07-20 02:32:18 【问题描述】:

我有以下类结构

public class ClassA

    public ClassB objB;


Public class ClassB

    public ListOfData objListofData;


public class ListOfData 

    public Employee objEmp;


public class Employee

    public string FirstName;
    public string LastName;

上述层次结构的返回类型将是

"ClassA":
    "classB":
        "ListOfData":
            "employee":
                "FirstName":"David",
                "LastName" :"Peter"
            
        
    

我想将员工类映射到 EmployeeViewModel,其中员工的名字和姓氏将映射到employeeViewModel FullName 属性。我可以通过以下代码来实现这一点

public class EmployeeViewModel

    public FullName;



CreateMap<Employee,EmployeeViewModel>()
                         .ForMember(dest => dest.FullName,
                                    opts => opts.MapFrom(
                                        src => string.Format("0 1",
                                        src.FirstName, src.LastName)));

现在如何返回具有 EmployeeViewModel 映射的原始 classA 对象。像这样的??

"ClassA":
    "classB":
        "ListOfData":
            "EmployeeViewModel":
                "FullName":"David Peter"
            
        
    

【问题讨论】:

【参考方案1】:

在花费大量时间阅读文档后,我发现解决方案是我的方法

a)像这样创建类源类和目标类

public class ClassA

    public ClassB objB;


Public class ClassB

    public ListOfData objListofData;


public class ListOfData 

    public Employee objEmp;


public class Employee

    public string FirstName;
    public string LastName;

------------------------------------
public class DestClassA

    public DestClassB objB;


Public class DestClassB

    public DestListOfData objListofData;


public class DestListOfData 

    public DestEmployee objEmp;


public class EmployeeViewModel

    public string FullName;

b)创建映射

var config = new MapperConfiguration(cfg => 

    cfg.CreateMap<Employee, EmployeeViewModel>().
                ForMember(dest => dest.FullName, opts => opts.MapFrom(
                    src => string.Format("0 - 1",
                    src.FirstName, src.LastName)));
    cfg.CreateMap<ListOfData, DestListOfData>();
    cfg.CreateMap<GetFundFamilyOutOfBalanceTotalsResponse, GetFundFamilyOutOfBalanceTotalsResponseMapped>();
    cfg.CreateMap<ClassB, DestClassB>();
    cfg.CreateMap<ClassA, DestClassA>();
);

//这是为了确保你的映射是正确的

config.AssertConfigurationIsValid();

//你可以测试预期的objected是否是automapper创建的,映射后添加如下代码

var mapper = config.CreateMapper();
var output = new ClassA

    objClassB = new ClassB
    
        objListofData = new ListOfData 
        
            Employee = new []
               
                    new Employee   FirstName = "David", LastName ="Peter" 
               
        
    
;

var destMap = mapper.Map<DestClassA>(output);

//如果你探索这个对象,你会发现像这样的对象结构

"ClassA":
    "classB":
        "ListOfData":
            "EmployeeViewModel":
                "FullName":"David Peter"
            
        
    

【讨论】:

以上是关于Automapper 映射嵌套对象的主要内容,如果未能解决你的问题,请参考以下文章

用于封装的 AutoMapper 和嵌套对象映射

C# automapper 嵌套对象条件映射

如何使用 AutoMapper 使用 EntityFramework 使用嵌套列表更新对象?

0.AutoMapper

C#AutoMapper 使用手册

AutoMapper 静态类不映射列表和嵌套实体