I have two entities: Order & OrderDTO And I'm using AutoMapper to map them together.
Based on some conditions I want these entities to be mapped differently.
In fact I want two or more different mapping rules (CreateMap
) for these entities.
And When calling Map
function I want to tell the engine which mapping rule to use.
Thanks to this question: Using the instance version of CreateMap and Map with a WCF service? one approach is using a different instance of mapper so each one can has it's 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 any better solution?
As mentioned by Jimmy Bogard (Creator of AutoMapper) here: Using Profiles in Automapper to map the same types with different logic :
You're better off creating separate Configuration objects, and creating a separate MappingEngine for each. The Mapper class is merely a static facade over each of those, with some lifecycle management.
What lifecycle management is needed to be done?