Dual Mapping

Using Automapper 5.0.2.0 I am trying to map from TypeA to TypeB:

public class TypeA
{
    public double Length { get; set; }
}


public class TypeB
{
    public Distance Length { get; set; }
}

I am assuming that the length is saved in inches and created this mapping profile:

public class CalculationProfile : Profile
{
    public CalculationProfile()
    {
       CreateMap<TypeA, TypeB>()
            .ForMember(dest => dest.Length,
                       opt => opt.MapFrom(src => new Distance(src.Length, "Inch")))
    }
}

and I use it as such:

Mapper.Initialize(configuration =>
{
    configuration.AddProfile(new CalculationProfile());
});

var typeA = new TypeA(){Length = 1.0};

var typeB = Mapper.Map<TypeB>(typeA);

However, this last line causes the following error:

AutoMapper.AutoMapperMappingException was unhandled by user code
  HResult=-2146233088
  Message=Missing type map configuration or unsupported mapping.

Mapping types:
Double -> Distance
System.Double -> UnitClassLibrary.DistanceUnit.Distance
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at lambda_method(Closure , Double , Distance , ResolutionContext )
       at lambda_method(Closure , Object , Object , ResolutionContext )
       at TestProject.AutoMapperTests.FromPersistenceObjectToCalculationModel_Test() in C:\...\AutoMapperTests.cs:line 69
  InnerException: 

This is similar to the super-simple case where Automapper is being processed, however I cannot fix the error. Any suggestions are welcome.

+4
source share
1 answer

You really need a new type conversion between dual and remote:

CreateMap<double, Distance>().ConvertUsing(src => new Distance(src, "Inch"));
0
source

Source: https://habr.com/ru/post/1649203/


All Articles