Mapowanie jednej klasy źródłowej na wiele klas pochodnych za pomocą automapper
Załóżmy, że mam klasę źródłową:
public class Source
{
//Several properties that can be mapped to DerivedBase and its subclasses
}
A niektóre klasy docelowe:
public class DestinationBase
{
//Several properties
}
public class DestinationDerived1 : DestinationBase
{
//Several properties
}
public class DestinationDerived2 : DestinationBase
{
//Several properties
}
Następnie chcę, aby pochodne klasy docelowe dziedziczyły konfigurację automapper klasy baseclass, ponieważ nie chcę jej powtarzać, czy jest jakiś sposób, aby to osiągnąć?
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(...);
Kiedy to piszę, w ogóle nie używa mapowań bazowych, a dołączenie nie wydaje mi się pomocne.
Edytuj: Oto co otrzymuję:
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: znaleziono niezmapowanych członków. Przejrzyj poniższe typy i członków.
Dodaj niestandardowe wyrażenie mapowania, zignoruj, dodaj niestandardowy przelicznik lub zmodyfikuj typ źródłowy / docelowyŹródło -> DestinationDerived1 (lista członków docelowych)Test3