Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper bidirectional mapping

If I want to do bi-directional mapping, do I need to create two mapping?

Mapper.CreateMap<A, B>() and Mapper.CreateMap<B, A>()?

like image 610
Benny Avatar asked Mar 13 '10 16:03

Benny


People also ask

Is AutoMapper bidirectional?

What is AutoMapper Reverse Mapping in C#? The Automapper Reverse Mapping is nothing but the two-way mapping which is also called as bidirectional mapping.

Does AutoMapper work both ways?

Yes, or you can call CreateMap<ModelClass, ViewModelClass>(). ReverseMap() .

What does ReverseMap do in AutoMapper?

By calling ReverseMap , AutoMapper creates a reverse mapping configuration that includes unflattening: var customer = new Customer { Name = "Bob" }; var order = new Order { Customer = customer, Total = 15.8m }; var orderDto = mapper.

What is AutoMapper good for?

AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.


1 Answers

Yes, but if you find yourself doing this often:

public static class AutoMapperExtensions {     public static void Bidirectional<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)     {         Mapper.CreateMap<TDestination, TSource>();     } } 

then:

Mapper.CreateMap<A, B>().Bidirectional(); 
like image 187
Eric Hauser Avatar answered Sep 18 '22 18:09

Eric Hauser