本文介绍了Ninject何时停用瞬态范围对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Ninject中的对象与InTransientScope()绑定时,该对象不会放入缓存中,因为它是临时的,并且没有作用域.

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.

完成对象后,我可以调用kernel.Release(obj);这将传递到Cache,在其中检索缓存的项目并使用缓存的条目调用Pipeline.Deactivate.

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?

推荐答案

您的假设是正确的.在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.

这篇关于Ninject何时停用瞬态范围对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:18