I have a site which uses a C# class file, and at the top of the file, I have:
JTSEntities database = new JTSEntities(); (an ADO.NET thingy).
I have it right up the top because I don’t really feel like writing the same thing over and over again.
But it raises one question… Because it’s up the top there, how will it get disposed, and when – and how can I dispose of it (or do I need to) when the user closes the page?
Ok so, based on the type of the attribute, we can say that it’s an
instance attribute. This means that this attribute will have the same lifecyle as the object that has it, unless its instance is passed to another object.If it’s protected, then only the owner object and its childs (that is, objects that inherit from this class) will have access to it. If you don’t pass it as a reference argument, then we’re ok with the first statement: it will be cleaned by the GC as soon as the object is cleaned.
If you just use this object on a regular webform page lifecycle, then you don’t have to worry with disposing it. The page lifecycle will do it for you.
Regards