對於AutoMapper,我實現了其工作原理如下,但重複的映射代碼:Automapper基類映射
cfg.CreateMap<RmsOelEntryUnlinkedPersonInputDto, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked>()
.ForMember(dest => dest.DateOfBirth, opts => opts.MapFrom(src => FormatDateType(src.DateOfBirth)))
.ForMember(dest => dest.EffectiveFromTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveFromTime)))
.ForMember(dest => dest.EffectiveToTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveToTime)));
cfg.CreateMap<RmsOelEntryUnlinkedAddressInputDto, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked>()
.ForMember(dest => dest.EffectiveFromTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveFromTime)))
.ForMember(dest => dest.EffectiveToTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveToTime)));
兩個RmsOelEntryUnlinkedPersonInputDto
和RmsOelEntryUnlinkedAddressInputDto
從RmsOelEntryInvolvedEntityBaseDto
繼承這個基類具有屬性EffectiveFromTime
和EffectiveToTime
。
我不想重複如上所示映射EffectiveFromTime
和EffectiveToTime
。
但是,AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked
和AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked
是自動生成的並且沒有基類。因此我看不到我如何使用AutoMapper的「包含」映射選項。
如何優化此刪除重複的映射?
感謝。在您的擴展方法中,我看到目標被約束爲AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked,但它也需要映射AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked。這將如何工作? – Howard
查看更新的答案:)如果生成的類是部分的,請打開問題? –
當然,如果這種情況比兩種映射更普遍,那麼這樣做會付出代價。 –