2016-12-15 60 views
1

我正在使用AutoMapper将我自己的复杂类型转换为其他Web服务API使用的稍微复杂的类型。使用多态性自动映射复杂类型?

这里是我的类型:

namespace Source 
{ 
    public class Order 
    { 
     public int OrderID { get; set; } 
     public Client Client { get; set; } 
     ... 
    } 

    public class ExistingClient : Client 
    { 
     public int ClientID { get; set; } 
     ... 
    } 

    public class NewClient : Client 
    { 
     public string Name { get; set; } 
     ... 
    } 
} 

,我需要这些转换为以下

namespace Target 
{ 
    public class WebOrder 
    { 
     public int OrderID { get; set; } 
     public WebClient { get; set; } 
     ... 
    } 

    public class WebClient 
    { 
     public object Item // instance of NewWebClient or ExistingWebClient 
    } 

    public class ExistingWebClient 
    { 
     public int ClientID { get; set; } 
     ... 
    } 

    public class NewWebClient 
    { 
     public string Name { get; set; } 
     ... 
    } 
} 

我创建了一个AutoMapper轮廓,其中包括以下CreateMap调用(加上一些更多的)

CreateMap<Source.ExistingClient, Target.ExistingWebClient>(); 
CreateMap<Source.NewClient, Target.NewWebClient>(); 
CreateMap<Source.Client, Target.WebClient>(); 

但我被困在如何使用多态性与MapTo()ResolveUsing()正确设置项目属性,缺少用每个子类型的单独映射替换最后一行,这似乎笨拙和重复。

CreateMap<Source.NewClient, Target.WebClient>() 
    .ForMember(d => d.Item, o => o.MapFrom(s => 
     Mapper.Map<Source.NewClient, Target.NewWebClient>(s))); 

CreateMap<Source.ExistingClient, Target.WebClient>() 
    .ForMember(d => d.Item, o => o.MapFrom(s => 
     Mapper.Map<Source.ExistingClient, Target.ExistingWebClient>(s))); 

回答

0

我不知道这是否是最好的方式来做到这一点,但我最终决定使用自定义解析器类。

public class ClientResolver : IValueResolver<Source.Client, Target.ClientWeb, object> 
{ 
    public object Resolve(Source.Client source, Target.ClientWeb destination, object destMember, ResolutionContext context) 
    { 
     if (source is Source.NewClient) 
     { 
      return Mapper.Map<Source.NewClient, Target.NewClientWeb>(source as Source.NewClient); 
     } 
     else if (source is Source.ExistingClient) 
     { 
      return Mapper.Map<Source.ExistingClient, Target.ExistingClientWeb>(source as Source.ExistingClient); 
     } 
     return null; 
    } 
}