简体   繁体   中英

Automapper 5.1.1 inheritance mapping

Hello I cant get my mappings working for inherited class. Idea is to create map for base object and interface only once, and when child classes implements their own members, configure mapping nly for those members that are not defined in base class or intrface.

Let me begin with sample code.

public class DtoClass {
   public string Field1 { get; set; }
   public string Field2 { get; set; }
   public string Field3 { get; set; }
}

public interface IField3 {
   public string EntityField3 { get; set; }
}

public class BaseEntityClass {
   public string EntityField1 { get; set; }
}

public class ChildEntityClass : BaseEntityClass, IField3  {
   public string EntityField2 { get; set; }
   public string EntityField3 { get; set; }
}


CreateMap<BaseEntityClass, DtoClass>()
   .ForMember(c => c.Field1 , m => m.MapFrom(a => a.EntityField1))
   .Include<ChildEntityClass, DtoClass>();

CreateMap<IField3, DtoClass>()
   .ForMember(c => c.Field3 , m => m.MapFrom(a => a.EntityField3));

CreateMap<ChildEntityClass, DtoClass>()
   .ForMember(c => c.Field2 , m => m.MapFrom(a => a.EntityField2));

Attached code dosnt work ofcourse. when calling :

AutoMapper.Mapper.Map<ChildEntityClass, DtoClass>(instanceOfChildEntityClass);

I get only mapped members that are defined in CreateMap<ChildEntityClass, DtoClass>().

Any idea how to implement mappings for base class and interfaces only once? And yes i want to map all types ChildEntityClass, BaseEntityClass and IField3 to DtoClass.

Any hints are welcome for elegant configuration such mappings.

Edit: I remove unecssary IncludeBase from subclass for clarity, but none of both
- IncludeBase in subclass - Include in base class Works for me. What can cause such problems ?

You should not be using .IncludeBase AND .Include - pick one and stick with it. I prefer .IncludeBase , as I think it makes more sense to define in the subclass. In your case, you cannot reference IField3 using Include because there is no implicit conversion.

The following code works using IncludeBase for me

CreateMap<BaseEntityClass, DtoClass>()
    .ForMember(dest => dest.Field1, opt => opt.MapFrom(src => src.EntityField1))
    ;

CreateMap<IField3, DtoClass>()
    .ForMember(dest => dest.Field3, opt => opt.MapFrom(src => src.EntityField3))
    ;

CreateMap<ChildEntityClass, DtoClass>()
    .ForMember(dest => dest.Field2, opt => opt.MapFrom(src => src.EntityField2))
    .IncludeBase<BaseEntityClass, DtoClass>()
    .IncludeBase<IField3, DtoClass>()
    ;

As it often happens issue were outside the scope i delivered in sample code.

In my project in initialization method was hidden invocation of something like:

        foreach (string propName in map.GetUnmappedPropertyNames())
        {
            expr.ForMember(propName, opt => opt.Ignore());
        }

So all columns not mapped in subclass were automatically ignored even when invoking mapping for base type. Simple yet problematic.

Such code as above were probably aded for Mapper.Configuration.AssertConfigurationIsValid(); to pass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM