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)));