2

The Scenario is as follows:

Source Object:

public class personSrc
{
    public string name {get; set;}
    public string phone {get; set;}
    public string HomeAddress {get; set;}
    public string OfficeAddress {get; set;}
}

Destination Object:

public class personDest
{
    public string name {get; set;}
    public string phone {get; set}
    public List<address> addresses {get; set;}
}

public class address
{
    public string location {get; set;}
    public addressType type {get; set;}
}
public enum addressType {Office, Home}

so in order to map the address members from source into destination i made this custom resolver:

public class CustomResolver : IValueResolver<personSrc, personDest, List<address>>
{
  public List<address> Resolve(personSrc source, personDest destination, List<address> destMember, ResolutionContext context)
  {
    List<address> result = new List<adress>();
    if(!String.IsNullOrEmpty(source.HomeAddress))
    {
       result.add(new Address{
         location = source.HomeAddress,
         type = addressType.Home
       });
    }
    if(!String.IsNullOrEmpty(source.OfficeAddress))
    {
       result.add(new Address{
         location = source.OfficeAddress,
         type = addressType.Office
       });
    }
    return result;
  }
}

the issue that i am facing is that i cannot seem to pass personSrc class when i am defining the profiler which i wrote like this :

CreateMap<personSrc, personDest>()
    .ForMember(dest => dest.name, opt => opt.MapFrom(src => src.name))
    .ForMember(dest => dest.phone, opt => opt.MapFrom(src => src.phone))
    .ForMember(dest => dest.addresses, opt => opt.MapFrom<customResolver>(src => src) // this line is not working
    .ForMember(dest => dest.addresses, opt => opt.MapFrom<customResolver>() // this line is also not working

most likely my profile is not properly implemented however i cannot seem to find an example of a proper implementation.

1 Answer 1

2

Your profile is correct. I have tried your example and I have managed to get values maped to your destination class and your profile should look like this:

public class CustomProfiler : Profile
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="CustomerProfile"/> class.
    /// </summary>
    public CustomProfiler()
    {
      CreateMap<personSrc, personDest>()
     .ForMember(dest => dest.name, opt => opt.MapFrom(src => src.name))
     .ForMember(dest => dest.phone, opt => opt.MapFrom(src => src.phone))
     .ForMember(dest => dest.addresses, opt => opt.MapFrom<CustomResolver>());
    }
  }

I have tried it with the example:

var personSrc = new personSrc { HomeAddress = "HomeAddress", OfficeAddress = "OfficeAddress", name = "name", phone = "phone" };
      var personDest = mapper.Map<personDest>(personSrc);

You were on the right track. Can you try it and let me know if that works for you?

1
  • yes it is correct i messed up by creating an interface for the IValueResolver that was conflicting with the Automapper Interface
    – maces13
    Commented Jun 25, 2020 at 10:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.