温莎城堡 - 我不得不释放单身或非一次性临时对象? [英] Castle Windsor - Do I have to release singleton or non-disposable transient objects?

查看:312
本文介绍了温莎城堡 - 我不得不释放单身或非一次性临时对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助城堡维基在几个地方,我应该始终调用container.Release()为组件解析说通过容器。这显然​​使得复杂的生活方式的管理技术(如LifeStyle.Pooled),或者使用感时,专门设施...



但是我真的不得不释放?单(其中活到容器配置)和非一次性临时对象如果我通过发行()调用临时对象或单身加强这些调用似乎是多余的 - .eg在不执行IDisposable的内核简单地注意到它有没有轨道的对象,并返回临时对象的情况下,...



有似乎的概念的部分负担跟踪,可能在解析一个瞬时对象被构建为其他一次性部件间接的引用。我明白,这是需要释放的瞬态对象,如果你不知道100%,他们是否有这样的间接依赖性与否。这是敦促所有城堡用户的主要原因的总是的组件版本?


解决方案

城堡Wiki是这里有点严格 - 想成为安全,而不是遗憾。它可以使用一些措辞有可能



反正 - 这里是它的工作原理



温莎(默认)跟踪大部分组件,并将其持有提及他们从收集他们停止垃圾收集器。这是不是一个错误 - 这是一个功能和极其有用和强大的。
在多数情况下,你不应该假定如果一个组件将被跟踪或没有。有一次性的依赖关系的非一次性部分也可被追踪。这是一般规则:组件,无论是自己的一些他们的依赖有任何退役的步骤默认情况下,发布政策温莎的跟踪



<。 。p>现在,这里的地方寿命发挥作用的部分。




  • 辛格尔顿 - 顾名思义单是在语境中的全局集装箱 - 当你第一次要求他们和住集装箱的生命周期的其余部分(这意味着,直到容器得到处理),他们获得创造。如果你看到的文件,它实际上说,实际上释放单身没有做任何事情。只有当容器被布置在退役你的一生组件的担忧将调用


  • 每(背景:web请求/ WCF会话/) - 自对象在一个定义良好的情况下分享了一个良好定义结束,上下文的结束将释放你的组件照顾


  • 瞬态 - 这就是真正的问题可能蠕变,由于短暂的组件没有一辈子的任何最终武断结束,您可以在应用程序的生命周期中产生的实例堆,还有比是明确的,说给容器哎,我没有别的办法中号不会再使用这个对象了,随意摆脱它,感谢所有的鱼。




现在为什么文档建议您还是释放组件的原因是,使用组件的代码应该不是真的知道一个组件的生命周期是什么。
并不总是这样的,常常在应用有一些自然地适合的生活方式的组件。然而,在一般情况下,就像我说的这是关于是安全的,而不是遗憾。



另一件事是,你叫解析发布。你应该永远只能发布解析



当您使用容器以类似的方式怎么我这样做,您可能没有在任何地方拨打发布在你的代码。您将解析你的根,但的Dispose 容器本身将释放它的照顾。您可能会也含蓄地解决其他组件通过类型化的工厂,在这种情况下的你也应该释放他们(通过工厂),而这通常是它。



所以,最终的结果是,它并不像它听起来起初是可怕的。


The Castle wiki says at several places I should ALWAYS call container.Release() for components resolved through the container. This obviously makes sense for sophisticated life-style management techniques (e.g. LifeStyle.Pooled) or when using specialized facilities...

But do I really have to release singleton (which live until the container is disposed) and non-disposable transient objects? If I step through Release() calls for transient objects or singletons these calls seem to be superfluous - .e.g. in the case of transient objects not implementing IDisposable the kernel simply notices that it has no track of the object and returns...

There seems to be the concept of a "component burden" to track "indirect" references to other disposable components that might be constructed while resolving a transient object. I understand that it is necessary to release transient objects if you do not know 100% whether they have such indirect dependencies or not. Is this the main reason to "urge" all Castle users to ALWAYS release components?

解决方案

Castle Wiki is a bit strict here - trying to be safe rather than sorry. It could use some rewording there probably.

Anyway - here's how it works.

Windsor (by default) tracks most components and it holds reference to them which stops Garbage Collector from collecting them. This is not a bug - it's a feature, and a extremely useful and powerful one. In most cases you should not assume if a component will be tracked or not. A non-disposable component that has disposable dependencies will also be tracked. This is in general rule: "components that either themselves of some of their dependencies have any decommission steps are tracked by default release policy in Windsor".

Now, here's the part where lifetimes come into play.

  • Singleton - by definition singleton are "global" in the context of the container - they get created when you first request them and live for the rest of the lifetime of the container (which means until the container gets disposed). If you see documentation, it actually says that releasing singletons actually doesn't do anything. It's only when the container gets disposed that decommission concerns of your lifetime components will get called.

  • Per (context: web request/WCF Session/) - since the object is shared in a well defined context with a well define end, end of the context will take care of releasing your components.

  • Transient - That's where the real issues can creep in. Since transient components don't have any end arbitrary end of lifetime and you can produce heaps of their instances during the lifespan of the app, there's no other way than being explicit and saying to the container "hey, I'm not gonna use this object anymore, feel free to get rid of it, thanks for all the fish."

Now the reason why the documentation suggest always releasing the components is that the code that uses components should not really know what the lifetime of a component is. It is not always the case, and often in applications there are components that "naturally" fit a lifestyle. In general however, like I said it's about being safe rather than sorry.

Another thing is where you call the Resolve and Release. You should only ever Release what you Resolve.

When you use the container in similar manner to how I do it, you may not have to call Release anywhere in your code. You will Resolve your root, but Dispose of the container itself will take care of releasing it. You will likely also resolve other components implicitly via typed factories, and in this case you should also release them (via the factory), but that's usually it.

So the end result is, it's not as scary as it sounds at first.

这篇关于温莎城堡 - 我不得不释放单身或非一次性临时对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆