c# - AutoMapper 4.2 and Ninject 3.2 -
i'm updating project of mine use automapper 4.2, , i'm running breaking changes. while seem have resolved said changes, i'm not entirely convinced i've done in appropriate way.
in old code, have ninjectconfiguration
, , automapperconfiguration
class each loaded webactivator. in new version automapperconfiguration
drops out , instead instance mapperconfiguration
directly in ninjectconfiguration
class bindings happening, so:
private static void registerservices( ikernel kernel) { var profiles = assemblyhelper.gettypesinheriting<profile>(assembly.load("???.mappings")).select(activator.createinstance).cast<profile>(); var config = new mapperconfiguration( c => { foreach (var profile in profiles) { c.addprofile(profile); } }); kernel.bind<mapperconfiguration>().tomethod( c => config).insingletonscope(); kernel.bind<imapper>().tomethod( c => config.createmapper()).inrequestscope(); registermodules(kernel); }
so, appropriate way of binding automapper 4.2 using ninject? seems working far, want make sure.
in before imapper interface didn't existed in library had implement interface , class below , bound them singleton pattern.
public interface imapper { t map<t>(object objecttomap); } public class automapperadapter : imapper { public t map<t>(object objecttomap) { //mapper.map static method of library! return mapper.map<t>(objecttomap); } }
now bind library's imapper interface single instance of mapperconfiguration.createmapper()
the problem code tho, should use single instance(or ninject says, constant) bind.
// reminder var config = new mapperconfiguration( c => { foreach (var profile in profiles) { c.addprofile(profile); } }); // solution starts here var mapper = config.createmapper(); kernel.bind<imapper>().toconstant(mapper);
Comments
Post a Comment