2014-12-03 158 views
1

我在做第三級映射時遇到了問題。這些類如下所示:如何映射嵌套類型

public class Company { 
    public Contact MainContact { get; private set; } 
} 


public class Contact { 
    public PersonsName ContactName { get; private set; } 
} 

public class PersonsName { 
    public String FirstName { get; private set; } 
    public String LastName { get; private set; } 

} 

public class CreateCompanyViewModel { 
    [Required] 
    [MaxLength(25, ErrorMessage = "Only 25 characters are allowed.")] 
    [Display(Name = "First Name")] 
    public String FirstName { get; set; } 

    [Required] 
    [MaxLength(25, ErrorMessage = "Only 25 characters are allowed.")] 
    [Display(Name = "Last Name")] 
    public String LastName { get; set; } 
} 

我嘗試沒有成功如下:

Mapper.CreateMap<CreateCompanyViewModel, PersonsName>() 
    .ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName)) 
    .ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName)); 

Mapper.CreateMap<Contact, PersonsName>() 
    .ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.ContactName.FirstName)) 
    .ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.ContactName.LastName)); 

Mapper.CreateMap<CreateCompanyViewModel, Company>() 
    .ForMember(dest => dest.MainContact.ContactName.FirstName, opt => opt.MapFrom(src => src.FirstName)) 
    .ForMember(dest => dest.MainContact.ContactName.FirstName, opt => opt.MapFrom(src => src.LastName)); 

該錯誤消息我得到的是:

表達「DEST => dest.MainContact.ContactName .FirstName'必須解析爲頂級成員,而不是任何子對象的屬性。改爲在子類型或AfterMap選項上使用自定義解析器。 參數名:lambdaExpression

+0

[Automapper map into nested class]的可能重複(http://stackoverflow.com/questions/7924185/automapper-map-into-nested-class) – 2014-12-03 14:03:43

+0

標題就是誤導,它實際上是一個關於映射到多個類的問題,全部在頂層。 – 2014-12-06 10:07:44

+0

你能顯示'CreateCompanyViewModel'嗎? – 2014-12-06 19:50:21

回答

2

您可以使用以下配置:

/* Innermost mapping */ 
Mapper.CreateMap<CreateCompanyViewModel, PersonsName>(); 
// You don't need the individual member mappings since the names match. 

/* Second-level */ 
Mapper.CreateMap<CreateCompanyViewModel, Contact>() 
    .ForMember(dest => dest.ContactName, opt => opt.MapFrom(src => src)); 

/* Top level */ 
Mapper.CreateMap<CreateCompanyViewModel, Company>() 
    .ForMember(dest => dest.MainContact, opt => opt.MapFrom(src => src)); 

的想法是,你使用的電源類型(CreateCompanyViewModel)作爲源的嵌套類型爲好。您使用ForMember內部的MapFrom(src => src)呼叫完成此操作。

例子:https://dotnetfiddle.net/XBJMba

由於src => src位看上去有些奇怪,你可以創建一個擴展方法,讓你在做什麼,更直白:

public static class AutoMapperExtensions 
{ 
    public static void MapFromSelf<TSourceType>(
     this IMemberConfigurationExpression<TSourceType> expression) 
    { 
     expression.MapFrom(s => s); 
    } 
} 

用法:

Mapper.CreateMap<CreateCompanyViewModel, Contact>() 
    .ForMember(dest => dest.ContactName, opt => opt.MapFromSelf()); 

Mapper.CreateMap<CreateCompanyViewModel, Company>() 
    .ForMember(dest => dest.MainContact, opt => opt.MapFromSelf()); 
+0

謝謝安德魯。直到週一晚纔能有機會嘗試這個,但看起來很有趣! – 2014-12-06 23:07:35

+0

乾杯安德魯,工作的魅力。有一種感覺,解決方案會很簡單(可能不那麼明顯):​​) – 2014-12-31 14:01:01