Question

I'm a little confuse about the direction of the mapping.

This is the create mapping.

Mapper.CreateMap<Dimension, PostedDimensionViewModel>()
            .ForMember(dto => dto.inputDimension, opt => opt.MapFrom(p => p.DimensionName))
            .ForMember(dto => dto.inputDescription, opt => opt.MapFrom(p => p.Description));

Then I can use it this way to go from PostedDimensionViewModel => Dimension:

Dimension dimension = Mapper.Map<PostedDimensionViewModel, Dimension>(model);

What if, I want to go from Dimension => PostedDimensionViewModel, do I need another CreateMap or I can just use the same above mapping from both directions?

Thanks for helping.

Était-ce utile?

La solution

Just do this:

Mapper.CreateMap<Source, Destination>()
    /* your other stuff here */
    .ReverseMap();

There are limitations here, and I wouldn't recommend this except between two DTOs.

Autres conseils

Yes, you'll need to provide an additional map if you intend to go in the other direction. If you try to use AutoMapper without the extra map you'll get an exception:

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:

PostedDimensionViewModel -> Dimension

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top