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
Afshin gh
source share