Hi,
What is a good way to dynamically select a set of properties to map at run time? We are mapping object with 100 properties and we only want to map a set of properties based on the request from client. If client A request for properties P1, P2, we will map on property P1, P2 to the destination object. If client A request for properties P1, P3, we will map on property P1, P3 to the destination object.
We come up with the solution to use instance of ConfigurationStore and MappingEngine to dynamically select the set of properties to map as below
public static MappingEngine GetMappingEngine<TSource, TDestination>(List<string> excludedProperties)
{
ConfigurationStore store = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
store.AssertConfigurationIsValid();
MappingEngine engine = new MappingEngine(store);
var mapping = store.CreateMap<TSource, TDestination>();
foreach (var prop in excludedProperties)
{
mapping.ForMember(prop, opt => opt.Ignore());
}
return engine;
}
Are the return engines are independent of each other? Can we create a base instance and create new instance from that (e.g. copy it and then modify)? We do not want to change the base instant directly since it will impact the mapping in another thread.
Thanks,
David