Dynamically select set of properties to map

82 views
Skip to first unread message

David Nguyen

unread,
Mar 10, 2015, 9:11:50 PM3/10/15
to automapp...@googlegroups.com
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  

Jimmy Bogard

unread,
Mar 10, 2015, 9:26:50 PM3/10/15
to automapp...@googlegroups.com
Yikes. No, not out of the box. Why do you have this situation? I generally try to avoid this sort of scenario.

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Nguyen

unread,
Mar 12, 2015, 5:05:13 PM3/12/15
to automapp...@googlegroups.com
Hi Jimmy,

Thanks for replying. Is your answer for which of question? Is it 'yes' for the first one?

"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)? "

Thanks,
David

David Nguyen

unread,
Mar 12, 2015, 5:07:30 PM3/12/15
to automapp...@googlegroups.com
By the way, we has this situation since the object is so big and it take a signification time to load and map. We want to optimize the mapping time by load and return only requested properties.

Jimmy Bogard

unread,
Mar 13, 2015, 8:54:09 AM3/13/15
to automapp...@googlegroups.com
There are ways to conditionally map specific properties for queryable projections, but I've only really seen those for situations like OData. Are you using the queryable projections?

And again, I'd try to avoid this scenario in the first place - it's very strange to have endpoints only map certain properties from my experience.

David Nguyen

unread,
Mar 19, 2015, 1:05:39 PM3/19/15
to automapp...@googlegroups.com
Yeah, we are using OData, so we don't want to map based on the projections. Can you refer some ways to do so?

Thanks,
David
Reply all
Reply to author
Forward
0 new messages