Search code examples
c#automapper-3

Automapper foces subobjects to be mapped


I am trying to map a response object from webservice to a Class in my project. I thought the Automapper will map even the sub objects automatically, it does not unless and until is forcefully set for the member. Why should i do this ?

Mapper.CreateMap<GetIfpQuoteResponse.Quote, QuoteWSModel>() .ForMember(dest => dest.CarrierRate, opt => opt.MapFrom(src => src.Carriers)) .ForMember(dest => dest.DroppedCarriers, opt => opt.MapFrom(src => src.DroppedRates)) .ForMember(dest => dest.MemberPlans, opt => opt.MapFrom(src => src.MemberPlans));

Why Wont the automapper map my su bobjects when i mention the class mapping like this

Mapper.CreateMap<GetIfpQuoteResponse.Quote, QuoteWSModel>(); Mapper.CreateMap<GetIfpQuoteResponse.Quote.Carrier, CarrierRateModel>(); Mapper.CreateMap<GetIfpQuoteResponse.Quote.DroppedCarrier, DroppedCarrierModel>();


Solution

  • AutoMapper only map top level object.

    If your class is built in the following way it will not work:

    Class A
    {
      B b;
    }
    
    Class B
    {
    
    }
    

    Class A will not know how to map property B inside class A.

    To do this you will need to create a Profile class.

    Auto Mapper tutorial