Automapper intialisation

78 views
Skip to first unread message

Rick Spence

unread,
Nov 17, 2015, 7:27:12 AM11/17/15
to AutoMapper-users
I have this code to enable non public members to be mapped:

Mapper.Initialize(cfg =>

{

cfg.ShouldMapProperty = p => true;

cfg.ShouldMapField = f => !f.IsDefined(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute));

});


The removes existing maps. The trouble I have is when this is run, I may have already created some maps and those are lost by this call.


Is there anyway to call Initialize but have it preserve existing maps? Or alternatively is there any way to set these CFG properties without initializing?


I'm trying to relieve clients from my business classes from having to explicitly call an intialise, and want to set up the maps in static constructors.


Thanks

Jimmy Bogard

unread,
Nov 17, 2015, 1:40:16 PM11/17/15
to automapper-users
Yes, Initialize resets the mappings.

A better way to do what you're trying to do is to create Profile classes, and have Initialize use reflection metadata to find all of the Profile classes and automatically add them:



--
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.

Reply all
Reply to author
Forward
0 new messages