Automapper: bidirectional mapping with ReverseMap() and ForMember()
ReverseMap
returns an IMappingExpression
that represents the reversal of the mapping. Once you call, it subsequent calls will be for configuring the reversal of the map.
Here's an example:
Mapper.CreateMap<CartItemDto, CartItemModel>()
.ForMember(dest => dest.ExtendedCost, opt => opt.Ignore())
.ReverseMap()
.ForMember(dest => dest.Pricing, opt => opt.Ignore())
This will ignore the Pricing
field in the reverse direction.
You could define your configuration like this:
Mapper.CreateMap<PartTwo, PartTwoViewModel>()
.ForMember(dst => dst.PartInteger, opt => opt.MapFrom(src => src.Integer));
Mapper.CreateMap<PartTwoViewModel, PartTwo>()
.ForMember(dst => dst.Integer, opt => opt.MapFrom(src => src.PartInteger));
UPDATE
Here is the commit where ReverseMap
was initially implemented. From what I can see in the code, it only creates a simple reverse mapping. For example, in this case it would automatically configure the equivalent of:
Mapper.CreateMap<PartTwoViewModel, PartTwo>();
To get anything more complex, I'm afraid that you're going to have to configure it manually.