我正在使用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()
的多态来正确设置Item属性,而不是用每个子类型的单独映射替换最后一行,这看起来很笨拙重复的。
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 :(得分: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;
}
}