Ninject: How to access root object of NamedScope from factory

好久不见. 提交于 2019-12-06 12:35:59

Yes you're right, out of the box Ninject can't do .DefinesNamedScope().InNamedScope(). Except maybe for late "creation" (factory, lazy) this couldn't work anyway, because it would create a cyclic dependency.

The simplest way to achieve what you want is to create a "root of the root"... well just one class ActualRoot which is bound with DefinesNamedScope() and gets an IRootScope injected, which again will be bound with .InNamedScope(). The bad thing about this is, that you will need to inject/Get<> an ActualRoot instead of a IRootScope.

As far as i remember, what you can also do instead, is:

Bind<IRootScope>().To<RootScope>()
    .InNamedScope(scopeName);

and then retrieve it as follows:

IResolutionRoot.Get<IRootScope>(new NamedScopeParameter(scopeName));

This way you don't need a DefinesNamedScope().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!