Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper inheritance -- reusing maps

Tags:

c#

automapper

I am trying to use automapper to create a single map for a parent object and to reuse this amongst its children.

For child properties I only want to map the extra fields.

Is this possible? The code I have looks like this

CreateCalculationMap(message);  ---- This does the BASE parent mappping.
    Mapper.CreateMap<QuoteMessage, CalculationGipMessage>()                     -- This is the child.
        .Include<QuoteMessage, CalculationBase>()                               -- Include the parent?
        .ForMember(a => a.OngoingCommission, b => b.MapFrom(c => c.OngoingASF)) - Child
        .ForMember(a => a.SpecialRate, b => b.MapFrom(c => c.BlahBlah)));       - Child

Why does it keep telling me the parents properties are not mapped? Yet, I thought I included them in CreateCalculationMap(message); which contains

Mapper.CreateMap<QuoteMessage, CalculationBase>() ()
like image 420
Chris McKelt Avatar asked Jun 02 '11 09:06

Chris McKelt


People also ask

Does AutoMapper map both ways?

Yes, or you can call CreateMap<ModelClass, ViewModelClass>(). ReverseMap() .

What is reverse map in AutoMapper?

The Automapper Reverse Mapping is nothing but the two-way mapping which is also called as bidirectional mapping. As of now, the mapping we discussed are one directional means if we have two types let's say Type A and Type B, then we Map Type A with Type B.

How do I use AutoMapper to list a map?

How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members.

How do I ignore property mapping in AutoMapper?

So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.


1 Answers

FYI I figured this out

 public static IMappingExpression<A, T> ApplyBaseQuoteMapping<A, T>(this   IMappingExpression<A, T> iMappingExpression)
        where A : QuoteMessage
        where T : CalculationGipMessage
    {
        iMappingExpression
            .ForMember(a => a.LoginUserName, b=> b.MapFrom(c => c.LoginUserName))
            .ForMember(a => a.AssetTestExempt, b => b.Ignore())
            ;

        return iMappingExpression;
    }


Mapper.CreateMap<QuoteMessage, CalculationGipMessageChild>()
            .ApplyBaseQuoteMappingToOldCol()
             // do other mappings here
like image 64
Chris McKelt Avatar answered Oct 12 '22 22:10

Chris McKelt