Using Dispose on a Singleton to Cleanup Resources

Posted by ImperialLion on Stack Overflow See other posts from Stack Overflow or by ImperialLion
Published on 2010-05-28T17:48:39Z Indexed on 2010/05/28 17:51 UTC
Read the original article Hit count: 252

Filed under:
|
|

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.

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) Is implementing IDisposable on a singleton fundamentally a bad idea?

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) 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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about singleton