ソースクラスがあるとします:
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
}
次に、派生宛先クラスがベースクラスのオートマッパー構成を継承するようにしたいのは、それを繰り返す必要がないためです。これを達成する方法はありますか?
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:マップされていないメンバーが見つかりました。以下のタイプとメンバーを確認してください。
テスト3
派生マッピングをベースマッピングに含める:
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);
AutoMapper wikiの Mapping inheritance を参照してください。
更新:これは、正常に機能するクラスの完全なコードです。
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; }
}
更新(AutoMapperバグの回避策):
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));
Automapper 8.0の場合。
現在のバージョンには新しいメソッドがあります IncludeAllDerived
これが実際の例です:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, DestinationBase>()
.ForMember(dest => dest.Test3, opt => opt.MapFrom(src => src.Test))
.IncludeAllDerived();
cfg.CreateMap<Source, DestinationDerived1>()
.ForMember(dest => dest.Test4, opt => opt.MapFrom(src => src.Test2));
cfg.CreateMap<Source, DestinationDerived2>()
.ForMember(dest => dest.Test5, opt => opt.MapFrom(src => src.Test2));
});
var mapper = config.CreateMapper();
var source = new Source { Test = "SourceTestProperty", Test2 = "SourceTest2Property" };
var d1 = mapper.Map<DestinationDerived1>(source);
var d2 = mapper.Map<DestinationDerived2>(source);
Assert.Equal("SourceTestProperty", d1.Test3);
Assert.Equal("SourceTest2Property", d1.Test4);
Assert.Equal("SourceTestProperty", d2.Test3);
Assert.Equal("SourceTest2Property", d2.Test5);