如果注入的属性进行处理? [英] Should injected properties be disposed?

查看:122
本文介绍了如果注入的属性进行处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短最佳实践的问题:如果一个对象A被注入到另一个对象B,然后应对象B实现IDisposable和处置A B处置时?

Short best practice question: If an object A is injected into another object B, should then object B implement IDisposable and dispose A when B is disposed?

推荐答案

我一般会说没有;依赖注入的性质意味着注入的对象不很了解什么是注射的生命周期;在一定程度上,这是注射的定义。因此,我不认为注入的对象应该释放任何它被注射;在注入代码应为知道所有正在注入的对象的整个生命周期负责,并应能妥善处理他们的时候他们的一切操作完成,而不是之前。

I would generally say no; the nature of dependency injection means that the injected object does not know much about the lifecycle of what it was injected with; to some extent, this is the definition of injection. As such, I do not think the injected object should dispose whatever it was injected with; the injecting code should take the responsibility for knowing the complete lifecycle of all the objects it is injecting, and should be able to properly dispose of them when all operations on them are complete, and not before.

这篇关于如果注入的属性进行处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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