Singleton object disposal
I've been doing some playing around with POCO objects and EntityFramework. Because of this I have to write my own Context and Repository(s). I would like all repositories to use the same context instance so changes are shared between them. To facilitate this I coded my Context as a Singleton. This way instead of getting a new context and then passing it in the contstructor for all my repositories I can just have the default contstructor get the singleton instance.
My questions are these:
Do I need to dispose of a singleton?
Can I just leave it and rely on garbage collection?
If I need to dispose of it how do I do that?
Is this an acceptable practice or is there some reason I shouldn't use the singleton that I'm not aware of?开发者_Go百科
If you stay leave our class, GC dispose it only when your program will be shutted down. Regarding Do I need to dispose of a singleton?
this is your personal need. If you wan't to recreate your instance of an object, create DestroyInstance method and set _instance = null there. But I think that Singleton is a realization where your instance must leave always..
精彩评论