假設我有這些映射:Automapper合併/合併映射
var mapping1 = Mapper.CreateMap<Order, BaseDto>()
.ForMember(o => o.Referrer, m => m.Ignore());
var mapping2 = Mapper.CreateMap<Order, DetailDto>()
.ForMember(o => o.Price, m => m.Ignore());
var mapping3 = Mapper.CreateMap<Order, UpdateDto>()
.ForMember(o => o.Temp, m => m.Ignore());
的DTO的繼承:
DetailDto : BaseDto
UpdateDto : BaseDto
有什麼辦法合併/組合第一映射到mapping2和mapping3?
事情是這樣的:
var mapping1 = Mapper.CreateMap<Order, BaseDto>()
.ForMember(o => o.Referrer, m => m.Ignore());
var mapping2 = Mapper.CreateMap<Order, DetailDto>()
.Import(mapping1);
.ForMember(o => o.Price, m => m.Ignore());
var mapping3 = Mapper.CreateMap<Order, UpdateDto>()
.Import(mapping1);
.ForMember(o => o.Temp, m => m.Ignore());
似乎是.Include
功能並沒有做我想做的是以下不工作:
var mapping1 = Mapper.CreateMap<Order, BaseDto>()
.Include<Order, DetailDto>()
.Include<Order, UpdateDto>()
.ForMember(o => o.Referrer, m => m.Ignore());
var mapping2 = Mapper.CreateMap<Order, DetailDto>()
.ForMember(o => o.Price, m => m.Ignore());
var mapping3 = Mapper.CreateMap<Order, UpdateDto>()
.ForMember(o => o.Temp, m => m.Ignore());
更新:編輯問題更加具體
得出每種類型爲什麼你就不能這樣做的:var mapping1 = Mapper.CreateMap <訂單,OrderDto >() .ForMember(o => o.Referrer,m => m.Ignore())。ForMember(o => o.Price,m => m.Ignore());? – Icarus
@伊卡洛斯它有點複雜,因爲映射有不同的目的地。我更新了這個問題。 – Dennis