Automapper with resolver throwing "Error mapping types"

Keppy picture Keppy · Sep 2, 2016 · Viewed 19.2k times · Source

We have a class inside another class as a property which needs to be mapped using Automapper. We have written a resolver which will map the source class properties to destinationMember properties. I have written the below logic which is not working.

We are receiving the following error.

Error mapping types.

Mapping types: SubscriberDTO -> Subscriber ConsoleAutomapperTestHarness.SubscriberDTO -> ConsoleAutomapperTestHarness.Subscriber

Type Map configuration: SubscriberDTO -> Subscriber ConsoleAutomapperTestHarness.SubscriberDTO -> ConsoleAutomapperTestHarness.Subscriber

Property: SubscriberSettings

using AutoMapper; //5.1.1.0
using System;

namespace ConsoleAutomapperTestHarness
{
   public class Program
    {
        public static void Main(string[] args)
        {
            SubscriberDTO subDTO = new SubscriberDTO();
            subDTO.AllowAddPFA = true;
            subDTO.AllowAutoPay = true; ;
            subDTO.SubscriberID = 10000;
            subDTO.FirstName = "Kishor";

            new SubscriberAutoMapper();

            Subscriber sub = Mapper.Map<SubscriberDTO, Subscriber>(subDTO);
            Console.WriteLine(sub.SubscriberSettings.AllowAddPFA.ToString());
            Console.ReadLine();
        }
    }

    public class SubscriberAutoMapper
    {
        public SubscriberAutoMapper()
        {
            Mapper.Initialize(cfg => {
                cfg.CreateMap<SubscriberDTO, Subscriber>()
                .ForMember(dest => dest.SubscriberSettings, opt => opt.ResolveUsing<SubscriberAutoMapperResolver>());                
            });
            Mapper.AssertConfigurationIsValid();
        }
    }
    public class SubscriberAutoMapperResolver : IValueResolver<SubscriberDTO, Subscriber, Settings>
    {
        public Settings Resolve(SubscriberDTO source, Subscriber destination, Settings destMember, ResolutionContext context)
        {
            //line which is working.
        return new Settings() { AllowAddPFA = source.AllowAddPFA };

        //line which is not working
       // var result = context.Mapper.Map<SubscriberDTO, Settings>(source);
       // var result = Mapper.Map<SubscriberDTO, Settings>(source);
        //var result = Mapper.Map<SubscriberDTO, Settings>(source,destMember);
        //var result = context.Mapper.Map<SubscriberDTO, Settings>(source, destMember, context);
        //return result;           

        }
    }
    public class Subscriber
    {
        public int SubscriberID { get; set; }
        public Settings SubscriberSettings { get; set; }
        public string FirstName { get; set; }
    }
    public class Settings
    {
        public bool AllowEnrollment { get; set; }
        public bool AllowAutoPay { get; set; }
        public bool AllowAddPFA { get; set; }

    }

    public class SubscriberDTO
    {
        public int SubscriberID { get; set; }
        public string FirstName { get; set; }

        public bool AllowEnrollment { get; set; }
        public bool AllowAutoPay { get; set; }
        public bool AllowAddPFA { get; set; }
    }


}

Answer

Alexandru Marculescu picture Alexandru Marculescu · Sep 5, 2016

The ValueResolver seems overkill honestly, you can drop it completely and achieve the desired result with as little as this (given that the default AutoMapper behaviour makes it redundant to explicitly specify properties when they have the same name, as in most of your models basically):

Mapper.Initialize(cfg => {
    cfg.CreateMap<SubscriberDTO, Subscriber>()
        .ForMember(d => d.SubscriberSettings, o => o.MapFrom(s => s));
    cfg.CreateMap<SubscriberDTO, Settings>();   
});