简体   繁体   中英

Simple Injector - Inject specific implementation of interface to specific controller

I have two search services that implement the same interface. One of them is used to search only within a small set of internal Umbraco content, the other searches through both Umbraco and external content and uses other than default search engine. I'd like to keep both of them, as each of them performs well, for the set of data it uses.

Is it possible in Simple Injector to specify, when to inject which service? I'd like to register the first one for most of the controllers, basically as a default implementation. Then, I need to register the second one only for two specific controllers.

While browsing I've found a solution to this issue, but it's in Ninject:

Bind<ITeamRespository>().To<SoccerRepository>().WhenInjectedInto(typeof(SoccerController));

Is the same thing doable with Simple Injector?

The Simple Injector equivalent is:

container.RegisterConditional<ITeamRespository, SoccerRepository>(
    c => c.Consumer.ImplementationType == typeof(SoccerController));

container.RegisterConditional<ITeamRespository, FallbackRepository>(c => !c.Handled);

More information can be found in the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM