Automapper - ReverseMap () does not match

I have the following 2 classes:

public class ReferenceEngine
{
    public Guid ReferenceEngineId { get; set; }
    public string Description { get; set; }
    public int Horsepower { get; set; }
}

public class Engine
{
    public Guid Id { get; set; }
    public string Description { get; set; }
    public int Power { get; set; }
}

I use automapper to do the mapping against ReferenceEngine to Engine and vice versa. Note that the ReferenceEngineId/ Idand Horsepower/ properties Powerdo not have the same name.

The following mapping configuration is performed, and properties with different names are displayed successfully:

public static void ConfigureMapperWorking()
{
    AutoMapper.Mapper.CreateMap<ReferenceEngine, Engine>()
        .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description)).ReverseMap();

    AutoMapper.Mapper.CreateMap<ReferenceEngine, Engine>()
        .ForMember(dest => dest.Id, opt => opt.MapFrom(src => Guid.Parse(src.ReferenceEngineId.ToString())))
        .ForMember(dest => dest.Power, opt => opt.MapFrom(src => src.Horsepower));

    AutoMapper.Mapper.CreateMap<Engine, ReferenceEngine>()
        .ForMember(dest => dest.ReferenceEngineId, opt => opt.MapFrom(src => Guid.Parse(src.Id.ToString())))
        .ForMember(dest => dest.Horsepower, opt => opt.MapFrom(src => src.Power));
}

However, the following does not work, although I call the method ReverseMap()at the end:

public static void ConfigureMapperNotWorking()
{
    AutoMapper.Mapper.CreateMap<ReferenceEngine, Engine>()
        .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.ReferenceEngineId))
        .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
        .ForMember(dest => dest.Power, opt => opt.MapFrom(src => src.Horsepower)).ReverseMap();
}

My question is, when the property names are different, should the TSource-> TDestination and TDestination-> TSource match manually? I thought the goal ReverseMapwas to avoid having to manually specify a bi-directional mapping.

+4
1

ReverseMap . ,

Mapper.CreateMap<Engine, ReferenceEngine>();

Mapper.CreateMap<ReferenceEngine, Engine>();

- , .

+3

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