如何获取对象是否已在注入中检索
是否有可能知道 ninject
kernel
已经满足了特定的依赖关系?需要明确的是:
假设我们有这个模块:
Bind<IA>().To<A>();
Bind<IB>().To<B>();
以及一些“客户端”代码:
var a = kernel.Get<IA>();
// how to get here "true" for assumption: "IA was requested (once)"
// and "false" for: "IB was not requested ever"
Is it possible to know that particular dependency already has been satisfied by ninject
kernel
? To be clear:
Let's suppose we have this module:
Bind<IA>().To<A>();
Bind<IB>().To<B>();
And some "client"-code:
var a = kernel.Get<IA>();
// how to get here "true" for assumption: "IA was requested (once)"
// and "false" for: "IB was not requested ever"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在连贯绑定中使用 .Before* 和 .After* 来挂钩对某些内容的调用以将其标记为已使用。或者您可以查看 IDisposable 挂钩(请参阅源下载中的测试)。
http 中有很好的背景信息: //kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/ 也是
You could use .Before* and .After* in the fluent bindings to hook in a call to something to mark it used. Or you could look at the IDisposable hooks (see the tests in the source download).
There's good background info in http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/ too
答案在这里找到:在 Ninject 中,如何在使用 Bind<..>.ToSelf() 创建对象后在对象上运行自定义代码?
The answer was found here: In Ninject, how can I run custom code on an object after it is created with Bind<..>.ToSelf()?