c# - Create overall IoC container -


i use ioc container in project, project contains many submodules. , use 1 base ioc module. example: have 2 interfaces in submodule a ione , itwo interfaces

public interface itwo {      // code }  public interface ione {      // code } 

and simpleinjectorregistertypes class

public class simpleinjectorregistertypes : isimpleinjectorregistration     {         public void register(container container)         {             container.registersingle<itwo, two>();             container.registersingle<ione, one>();         }     } 

and main class, use logic

private static ione _one;         private static itwo _two;          static void main(string[] args)         {             container container = new container();             new simpleinjectorregistertypes().register(container);             _one= container.getinstance<ione>();             _two= container.getinstance<itwo>();         } 

it good, have submodule b have interfaces ithree , tfour , simpleinjectorregistertypes , main class. how write general ioc container submodules? p.s. ioc use bootstrapper

assume there 3 sub assemblies say, common, modulea , moduleb.

  • common has icommon interface , isimpleinjectorregistration interface.
  • modulea has commona implements icommon, , independent registrations(ione:one, itwo:two)
  • moduleb has commonb implements icommon, , registrations(ithree:three, ifour:four)

modulea register this:

public class registrationa : isimpleinjectorregistration {     public void register(container container)     {         container.register<ione, one>();         container.register<itwo, two>();         container.register<icommon, commona>();         container.registerall<icommon>(typeof(commona));     } } 

and moduleb register this:

public class registrationb : isimpleinjectorregistration {     public void register(container container)     {         container.register<ithree, three>();         container.register<ifour, four>();         container.register<icommon, commonb>();         container.registerall<icommon>(typeof (commonb));     } } 

now, in main module might this:

var container = new container(); new modulea.registrationa().register(container); new moduleb.registrationb().register(container); container.verify(); 

but fails on moduleb registration, because icommon duplicated in both registrations.

you can avoid duplicated registration , force replace latest 1 using options.allowoverridingregistrations:

var container2 = new container(); container2.options.allowoverridingregistrations = true; new modulea.registration().register(container2); new moduleb.registration().register(container2); container2.options.allowoverridingregistrations = false; container2.verify();  var commoninstance2 = container2.getinstance<icommon>(); var commoninstances2 = container2.getallinstances<icommon>(); 

as result, commoninstance2 instance of commonb , commoninstances2 sequence of icommon contains single commonb instance.

you might want commona icommon. or implementations of icommon ienumerable<icommon>:

var container3 = new container(); container3.options.allowoverridingregistrations = true; new modulea.registration().register(container3); new moduleb.registration().register(container3);  // can choose icommon should registered. container3.register<icommon, modulea.commona>();  // or collect implementation of icommon // (of course using assembly.gettypes() better solution) container3.registerall<icommon>(typeof (modulea.commona), typeof (moduleb.commonb));  container3.options.allowoverridingregistrations = false; container3.verify();  var commoninstance3 = container3.getinstance<icommon>(); var commoninstances3 = container3.getallinstances<icommon>(); 

as result, commoninstance3 commona instance , commoninstances3 contain both commona , commonb instances.

the point is, should keep simple when register dependencies. 1 place ideal, if have independent modules , each modules not aware of each others, 'main' knows both modules should configure registrations correctly.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -