c# – 使用automapper將一個源類對映到多個派生類
假設我有一個源類:
public class Source { //Several properties that can be mapped to DerivedBase and its subclasses }
和一些目的地類:
public class DestinationBase { //Several properties } public class DestinationDerived1 : DestinationBase { //Several properties } public class DestinationDerived2 : DestinationBase { //Several properties }
然後我希望派生的目標類繼承baseclass的automapper配置,因為我不想重複它,有沒有辦法實現這一點?
Mapper.CreateMap<Source, DestinationBase>() .ForMember(...) // Many more specific configurations that should not have to be repeated for the derived classes .ForMember(...); Mapper.CreateMap<Source, DestinationDerived1 >() .ForMember(...); Mapper.CreateMap<Source, DestinationDerived2 >() .ForMember(...);
當我這樣寫它,它根本不使用基本對映,包括似乎不幫助我.
編輯:
這是我得到的:
public class Source { public string Test { get; set; } public string Test2 { get; set; } } public class DestinationBase { public string Test3 { get; set; } } public class DestinationDerived1 : DestinationBase { public string Test4 { get; set; } } public class DestinationDerived2 : DestinationBase { public string Test5 { get; set; } }
Mapper.CreateMap<Source, DestinationBase>() .ForMember(d => d.Test3, e => e.MapFrom(s => s.Test)) .Include<Source, DestinationDerived1>() .Include<Source, DestinationDerived2>(); Mapper.CreateMap<Source, DestinationDerived1>() .ForMember(d => d.Test4, e => e.MapFrom(s => s.Test2)); Mapper.CreateMap<Source, DestinationDerived2>() .ForMember(d => d.Test5, e => e.MapFrom(s => s.Test2));
AutoMapper.AutoMapperConfigurationException:
未對映成員被發現.檢視下面的型別和成員.
新增自定義對映表示式,忽略,新增自定義解析器或修改源/目標型別
來源 – > DestinationDerived1(目的地成員列表)
TEST3
將派生的對映包含到基本對映中:
Mapper.CreateMap<Source, DestinationBase>() .ForMember(d => d.Id, op => op.MapFrom(s => s.Id)) // you can remove this .Include<Source, DestinationDerived1>() .Include<Source, DestinationDerived2>(); Mapper.CreateMap<Source, DestinationDerived1>() .ForMember(d => d.Name, op => op.MapFrom(s => s.Text)) .ForMember(d => d.Value2, op => op.MapFrom(s => s.Amount)); Mapper.CreateMap<Source, DestinationDerived2>() .ForMember(d => d.Value, op => op.MapFrom(s => s.Amount));
用法:
Mapper.AssertConfigurationIsValid(); var s = new Source() { Id = 2, Amount = 10M, Text = "foo" }; var d1 = Mapper.Map<DestinationDerived1>(s); var d2 = Mapper.Map<DestinationDerived2>(s);
參見ping-inheritance" rel="nofollow,noindex" target="_blank">Mapping inheritance AutoMapper維基.
更新:這是完整的程式碼,按照它應該工作.
public class Source { public int Id { get; set; } public string Text { get; set; } public decimal Amount { get; set; } } public class DestinationBase { public int Id { get; set; } } public class DestinationDerived1 : DestinationBase { public string Name { get; set; } public decimal Value2 { get; set; } } public class DestinationDerived2 : DestinationBase { public decimal Value { get; set; } }
UPDATE(AutoMapper bug的解決方法):
public static class Extensions { public static IMappingExpression<Source, TDestination> MapBase<TDestination>( this IMappingExpression<Source, TDestination> mapping) where TDestination: DestinationBase { // all base class mappings goes here return mapping.ForMember(d => d.Test3, e => e.MapFrom(s => s.Test)); } }
和所有對映:
Mapper.CreateMap<Source, DestinationBase>() .Include<Source, DestinationDerived1>() .Include<Source, DestinationDerived2>() .MapBase(); Mapper.CreateMap<Source, DestinationDerived1>() .MapBase() .ForMember(d => d.Test4, e => e.MapFrom(s => s.Test2)); Mapper.CreateMap<Source, DestinationDerived2>() .MapBase() .ForMember(d => d.Test5, e => e.MapFrom(s => s.Test2));
http://stackoverflow.com/questions/14705064/mapping-one-source-class-to-multiple-derived-classes-with-automapper