2011-11-16 65 views
5

映射DTO到域对象比方说,我有两个对象,我想映射:AutoMapper:与子对象

// Domain objects 
public class MyDomainObject 
{ 
    public string SimpleText { get; set; } 
    public int SimpleNumber { get; set; } 
    public MySubObject ComplexValue { get; set; } 
} 

public class MySubObject 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

// DTOs 
public class MyDto 
{ 
    public string SimpleText { get; set; } 
    public int SimpleNumber { get; set; } 
    public int ComplexValueId { get; set; } 
    public string ComplexValueName { get; set; } 
} 

// Mapping config 
Mapper.CreateMap<MyDomainObject, MyDto>(); 

THS将正常工作,而无需额外的配置,因为AutoMapper将着眼于驼峰规则和向下钻取。

现在我想映射DTO回域对象
Mapper.Map<MyDto, MyDomainObject>(dto, domainObj);

什么将最好的/简单的映射配置是实现呢?

+0

相关:http://stackoverflow.com/questions/3145062/using-automapper-to-unflatten-a-dto –

回答