何时在 Ninject 中停用瞬态范围对象?
当 Ninject 中的对象与 InTransientScope()
绑定时,该对象不会放入缓存中,因为它是,呃,瞬态的并且没有任何作用域。
处理完该对象后,我可以调用 kernel.Release(obj);它传递到缓存,在缓存中检索缓存的项目并使用缓存的条目调用 Pipeline.Deactivate 。
但由于瞬态对象没有被缓存,所以这种情况不会发生。我无法弄清楚在哪里(或谁)执行瞬态对象的停用。或者是假设瞬态对象仅被激活,并且如果我想要一个可停用的对象,我需要使用其他范围?
When an object in Ninject is bound with InTransientScope()
, the object isn't placed into the cache, since it's, er, transient and not scoped to anything.
When done with the object, I can call kernel.Release(obj)
; this passes through to the Cache where it retrieves the cached item and calls Pipeline.Deactivate
using the cached entry.
But since transient objects aren't cached, this doesn't happen. I haven't been able to figure out where (or who) performs the deactivation for transient objects. Or is the assumption that transient objects are only ever activated, and that if I want a deactivateable object, I need to use some other scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的假设是正确的。瞬态对象不会在 Ninject 中进行跟踪,也不会在停用管道中进行控制。您有责任清理瞬态实例。如果您希望内核管理您的实例,那么您需要使用内置作用域或自定义作用域。
Your assumptions are correct. Transient objects are not tracked in Ninject and not controlled in the deactivation pipeline. It is your responsibility to clean up transient instances. If you want the kernel to manage your instances, then you need to use a built-in scope or a custom scope.