问题描述
我的问题可能比与实际使用的IDisposable
的更多的是语义。我正在实现一个单例类是负责管理,它是应用程序的执行过程中创建一个数据库实例。当应用程序关闭该数据库应予以删除。
The question I have might be more to do with semantics than with the actual use of IDisposable
. I am working on implementing a singleton class that is in charge of managing a database instance that is created during the execution of the application. When the application closes this database should be deleted.
现在我有这个删除被一个清理处理()
单身的方法在应用程序调用时它正在缩小。当我写了)的文档清理(
我突然想到,我是描述什么的Dispose()
方法应用于即清理资源。我原本没有实施的IDisposable
,因为它在我的单身显得格格不入,因为我什么也没要处置单本身。目前还没有,但在未来可能是一个原因,这个清理()
可能会被调用,但单身应该将需要仍然存在。我想我可以包括 GC.SuppressFinalize(本);
的Dispose方法,使这个可行的。
Right now I have this delete being handled by a Cleanup()
method of the singleton that the application calls when it is closing. As I was writing the documentation for Cleanup()
it struck me that I was describing what a Dispose()
method should be used for i.e. cleaning up resources. I had originally not implemented IDisposable
because it seemed out of place in my singleton, because I didn't want anything to dispose the singleton itself. There isn't currently, but in the future might be a reason that this Cleanup()
might be called but the singleton should will need to still exist. I think I can include GC.SuppressFinalize(this);
in the Dispose method to make this feasible.
我的问题,因此是多分手:
My question therefore is multi-parted:
1)实施 IDisposable的
上一个单身本质上是一个坏主意?
1) Is implementing IDisposable
on a singleton fundamentally a bad idea?
2)难道我只是有一个清理混合语义这里()
代替的Dispose()
键,因为我处置的资源我真的应该用脱手?
2) Am I just mixing semantics here by having a Cleanup()
instead of a Dispose()
and since I'm disposing resources I really should use a dispose?
3)将实施'的Dispose()'与 GC.SuppressFinalize(本);
使它所以我的单身是不实际的情况下摧毁我希望它住在通话后清理数据库。
3) Will implementing 'Dispose()' with GC.SuppressFinalize(this);
make it so my singleton is not actually destroyed in the case I want it to live after a call to clean-up the database.
推荐答案
总之,如果你有一个独立的,并调用处理。任何任何对象尝试之后,要使用它的时间,将是利用一个布置状态的对象
In short, if you have a singleton and you call dispose. Any time that any object tries to use it after that, will be using an object in a disposed state.
现在把它在和涂布后的对象的处置用它做的,不一定是坏事。你必须要小心,不过,当你调用它。如果你真的关心的是清理和您只有一提到它,你可以把清理代码中的对象终结〜YourClass
这样,它才会被调用与.net是确保它不再需要(当应用程序关闭,如果它是一个真正的单身)。
Now putting it in, and disposing of the object after the application is done with it, isn't necessarily bad. You do have to be careful though when you call it. If you are really concerned with the cleanup and you have only one reference to it, you can put the clean up code in the objects finalizer ~YourClass
that way it will only be called with .Net is sure it is no longer needed (when the application closes, if it is a true singleton).
我是不是混语义通过这里有一个清理(
),而不是
的Dispose(),并因为我处理
资源我真的应该使用
处理?
是的,这只是语义。处置是显示有事情需要程序与对象完成后要及时清理标准。
Yes, this is just semantics. Dispose is the standard for showing there are things that need to be cleared up after the program is done with the object.
将实施'的Dispose()'与
GC.SuppressFinalize(本);做起来很
我的单实际上并没有被破坏
在我希望它一个
调用后活到清理数据库的情况。
没有这是什么意思是,当你调用Dispose方法,垃圾收集器不会调用对象的自定义终结。
No what this means is that when you call the dispose method, the Garbage Collector won't call the object's custom finalizer.
这篇关于在辛格尔顿利用处置清理资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!