0

I have four classes - two view models and two entities:

PhoneNumberTypeViewModel:

public class PhoneNumberTypeViewModel
{
    public int Id { get; private set; }

    public string Description { get; set; }

    public PhoneNumberTypeViewModel(int id, string description)
    {
        Id = id;
        Description = description;
    }
}

PhoneNumberViewModel:

public class PhoneNumberViewModel
{
    public string PhNumber { get; set; }

    public PhoneNumberTypeViewModel PhoneType { get; set; }

    public PhoneNumberViewModel(string phNumber, PhoneNumberTypeViewModel phoneType)
    {
        PhNumber = phNumber;
        PhoneType = phoneType;
    }
}

PhoneNumberType:

public class PhoneNumberType : ValueObject
{
    public static readonly PhoneNumberType Home = new PhoneNumberType(1, "Home");

    public static readonly PhoneNumberType Office = new PhoneNumberType(2, "Office");

    public static readonly PhoneNumberType Cell = new PhoneNumberType(3, "Cell");

    public static readonly PhoneNumberType[] AllStatuses = { Home, Office, Cell };

    public int Id { get; }
    public string Description { get; private set;}

    private PhoneNumberType()
    {
    }

    public PhoneNumberType(int id, string description)
    {
        Id = id;
        Description = description;
    }
}

PhoneNumber:

public class PhoneNumber : ValueObject
{
    public int Id { get;  private set; }

    [Phone]
    public string PhNumber { get; private set;}

    public PhoneNumberType PhoneType { get; }

    private PhoneNumber()
    {
    }

    public PhoneNumber(string phNumber, PhoneNumberType phoneType)
    {
        PhNumber = phNumber;
        PhoneType = phoneType;
    }
}

I use AutoMapper to map the classes:

var config1 = new MapperConfiguration(cfg => cfg.CreateMap<PhoneNumberType, PhoneNumberTypeViewModel>().ReverseMap());
var config2 = new MapperConfiguration(cfg => cfg.CreateMap<PhoneNumber, PhoneNumberViewModel>());

I then try to validate the configuration using:

            config1.AssertConfigurationIsValid();
            config2.AssertConfigurationIsValid();

The validation on config2 throws the following exception:

+       $exception  {"The following member on JobAssist.Services.ConnectionMgmt.API.Application.ViewModels.PhoneNumberViewModel cannot be mapped: \n\tPhoneType \nAdd a custom mapping expression, ignore, add a custom resolver, or modify the destination type JobAssist.Services.ConnectionMgmt.API.Application.ViewModels.PhoneNumberViewModel.\nContext:\n\tMapping to member PhoneType from JobAssist.Services.ConnectionMgmt.Domain.AggregatesModel.ConnectionManagerAggregate.PhoneNumber to JobAssist.Services.ConnectionMgmt.API.Application.ViewModels.PhoneNumberViewModel\nException of type 'AutoMapper.AutoMapperConfigurationException' was thrown."}   AutoMapper.AutoMapperConfigurationException

Config1 passes validation so why is the Config2 throwing this error?

Mike Lenart
  • 767
  • 1
  • 5
  • 19

1 Answers1

0

I found the issue: In the code above I had removed the checking for invalid parameters. In the constructor for PhoneNumberViewModel I was checking to see if the phoneType was null. I guess Automapper calls the setter method after constructing the entity.

Mike Lenart
  • 767
  • 1
  • 5
  • 19