asp.net - AutoMapper - Multiple Class Libraries -


i have asp.net web forms application , classlibrary, both of define own automapper.profiles classes. 1 example is:

public class mymappings : automapper.profile {     public mymappings() : base("mymappings")     {     }      protected override void configure()     {         mapper.createmap<dal.user, sessionuser>()             .formember(                 dest => dest.locationname,                 opt => opt.mapfrom(src => src.locationforuser.name));     } } 

in order both mappings configured , in order 1 mapping profile not overwrite others, correct should have 1 automapperconfig (defined in asp.net forms application) follows:?

namespace platformnet.mappings { public class automapperconfig {     public static void configure()     {         mapper.initialize(x =>         {             x.addprofile<mymappings>();             x.addprofile<assessmentclasslib.mappings.mymappings>();         });      } } } 

and call once global.asax follows?

void application_start(object sender, eventargs e) {     platformnet.mappings.automapperconfig.configure(); } 

i've got automapperconfig class in both web app , class library , calling both in turn global.asax 2nd appears overwrite configuration of first. need know if doing correctly. thanks.

the profiles aren't overriding each other, each call mapper.initialize resets previous automapper profiles. should call mapper.initialize 1 time.

the code below demonstrates this:

class program {     static void main(string[] args)     {         callinitonce();          mapper.reset();          callinittwice();     }      static void callinitonce()     {         // add profiles 1 time         mapper.initialize(x =>         {             x.addprofile<classatoclassbprofile>();             x.addprofile<emptyprofile>();         });          var classa = new classa()         {             myproperty = 2         };          var classb = mapper.map<classb>(classa);          debug.assert(classa.myproperty == classb.myproperty); // profiles work     }      static void callinittwice()     {         mapper.initialize(x =>         {             x.addprofile<classatoclassbprofile>();         });          // previous mapping profile lost         mapper.initialize(x =>         {             x.addprofile<emptyprofile>();          });          var classa = new classa()         {             myproperty = 2         };         var classb = mapper.map<classb>(classa);          debug.assert(classa.myproperty == classb.myproperty); // fail     } } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -