dependency injection - How does IoC container know which named instance to inject? -
when there multiple named implementations given interface, how container (i using unity in prism application) know 1 inject unless call container.resolve registered name? here simple example:
public interface idependencyclass { void dosomething(); } public class dependencyclassa : idependencyclass { void dosomething() { } } public class dependencyclassb : idependencyclass { void dosomething() { } } public interface iconsumer { void takeuserspecificaction(); } public class consumer : iconsumer { idependencyclass dependencyinstance; public consumer(idependencyclass _dependencyinstance) { dependencyinstance = _dependencyinstance; } public void takeuserspecificaction() { dependencyinstance.dosomething(); } } public class mybootstrapper : unitybootstrapper { protected override void configurecontainer() { base.configurecontainer(); container.registertype<idependencyclass, dependencyclassa>( "instancea" ); container.registertype<idependencyclass, dependencyclassb>( "instanceb" ); container.registertype<iconsumer, consumer>(); } }
and here mainviewmodel application. "raisesomecommand" command not enabled until user has logged in. when enabled, can execute reaiseconsumercommandrequest, in turn calls consumer. here viewmodel.
public class mainwindowviewmodel { private readonly iregionmanager regionmanager; private readonly iloginservice loginservice; private readonly iconsumer consumer; public icommand raisesomecommand { get; set; } public mainwindowviewmodel( iregionmanager regmgr, iloginservice _loginservice, iconsumer _consumer ) { regionmanager = regmgr; loginservice = _loginservice; consumer = _consumer; navigatecommand = new delegatecommand<string>( navigate ); loginviewrequest = new interactionrequest<inotification>(); raisesomecommand = new delegatecommand( raiseconsumercommandrequest ); } private void raiseconsumercommandrequest() { consumer.takeuserspecificaction(); } }
so, when execute
consumer.takeuserspecificaction();
which dependencyclass instance using? dependencyclassa or dependencyclassb. also, if want use dependencyclassb, need make happen. don't want call
container.reslove<idependencyclass>("instanceb")
in viewmodel because using container service locator. passing container reference around.
i have seen in code examples constructor parameter consumer class decorated dependency attribute below.
public class consumer { idependencyclass dependencyinstance; public consumer([dependency("instanceb")]idependencyclass _dependencyinstance) { dependencyinstance = _dependencyinstance; } }
but then, putting hard constraint on consumer use "instanceb" implementation. secondly, creating dependency unity. thirdly, have clone consumer class use "instancea" implementation. goes against dry principle.
i have heard these conditions application decisions , not ioc related logic. can agree argument. then, , how in application resolve right implementation without violating 1 rule or another?
i can't see how can inject right concrete instance unless choose use 1 of above 2 options. container.resolve or dependency attribute. can please?
Comments
Post a Comment