Different matching rules for the same object types in AutoMapper

I have two objects: Order and OrderDTO. I use AutoMapper to match them.

Based on some conditions , I want these objects to be displayed differently .

In fact, I want two or more different display rules ( CreateMap ) for these objects.

And when calling the Map function, I want to tell the engine what is used for the matching rule .

Thanks to this question: Using an instance of CreateMap and Map with WCF? one approach uses a different instance of mapper, so everyone can have their own mapping rules:

 var configuration = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers()); var mapper = new MappingEngine(configuration); configuration.CreateMap<Dto.Ticket, Entities.Ticket>() 

Do you have a better solution?

As Jimmy Bogard (Creator of AutoMapper) mentioned here: Using profiles in Automapper to map the same types to different logic :

You are better off creating separate Configuration objects and creating a separate MappingEngine for each. The Mapper class is just a static facade over each of them, with some lifecycle management.

What is needed for lifecycle management?

+8
c # asp.net-mvc automapper
source share
2 answers

I created a new instance of mapper and cached them in a common (static) parallel dictionary.

here is my code (vb.net):

mapper factory:

 Public Function CreateMapper() As IMapper Implements IMapperFactory.CreateMapper Dim nestedConfig = New ConfigurationStore(New TypeMapFactory, MapperRegistry.Mappers) Dim nestedMapper = New MappingEngine(nestedConfig) Return New AutomapperMapper(nestedConfig, nestedMapper) End Function 

different profiles for different scenarios:

 Private Shared _mapperInstances As New Concurrent.ConcurrentDictionary(Of String, IMapper) Public Shared ReadOnly Property Profile(profileName As String) As IMapper Get Return _mapperInstances.GetOrAdd(profileName, Function() _mapperFactory.CreateMapper) End Get End Property 

and mapping class:

 Friend Class AutomapperMapper Implements IMapper Private _configuration As ConfigurationStore Private _mapper As MappingEngine Public Sub New() _configuration = AutoMapper.Mapper.Configuration _mapper = AutoMapper.Mapper.Engine End Sub Public Sub New(configuration As ConfigurationStore, mapper As MappingEngine) _configuration = configuration _mapper = mapper End Sub Public Sub CreateMap(Of TSource, TDestination)() Implements IMapper.CreateMap _configuration.CreateMap(Of TSource, TDestination)() End Sub Public Function Map(Of TSource, TDestination)(source As TSource, destination As TDestination) As TDestination Implements IMapper.Map Return _mapper.Map(Of TSource, TDestination)(source, destination) End Function Public Function Map(Of TSource, TDestination)(source As TSource) As TDestination Implements IMapper.Map Return _mapper.Map(Of TSource, TDestination)(source) End Function End Class 
+3
source share

I ran into the same problem and found that AutoMapper was updated to 4.2.0 a month ago, starting with support for mappers instances created by different configurations, and static mapping functions are deprecated. Therefore, we do not need to realize ourselves from now on!

+1
source share

All Articles