As you might have gathered from the Subject, I've been experimenting with
using VBScript classes in my ASP pages to tidy the script up. Generally
things are going well.
Since I come from a C++ background, I have long made use of singleton
objects, in various forms, and I can see an obvious use for one here but not
sure if I can. I'll explain the situation:-
I have a cDataLink class, configured via a couple of Const defines for the
Provider type, database name etc (So I can easily set it according to
Access/SQL etc). In addition, all my other classes use 'lazy loading', only
going to a SQL SELECT to get information if they are asked for that data. To
create the cDataLink object, I can see two possibilities.
a) I can create it at the beginning of every page. Setting the object to
Nothing at the end will (I assume) call its Class_Terminate() and allow it
to clean up.
Although this would work, it means that each page will open the data
connection regardless of whether the other objects actually need it. It goes
against the grain of only opening a connection if you have to, and then at
the latest possible moment, especially if it then isn't required for that
particular parsing of the page.
b) Using the abilities provided by a well designed Singleton, the first
object to require the connection would create the cDataLink object by the
very nature of asking for it. Any data connection requests after that point
would return the existing connection object.
The obvious benefit of this is that if no class instance requests the
connection, it will never be created. One problem I foresee is in the
termination of the connection. Unless I actively Set the connection to
Nothing at the end of the page, I'm not sure if I can garantee that the
connection object will be terminated and released. This goes against the
design of a Singleton, which lingers uniquely just as long as it is required
and then goes.
c) Possibly I could do something based round Reference counts, like a COM
object, but that may be code for coding's sake.
On to my questions:-
a) Am I right to worry that a VBS class is not garanteed to call its
Terminate unless it is set to Nothing somewhere, or is it safe to assume it
will vanish when the page has finished parsing, running its Terminate() in
the process.
b) I have yet to find any sort of 'static' declaration for VBS classes
(although I may have been looking in the wrong place). This is almost
obligatory for creating a true Singleton type of object, since its one of
the tricks for ensuring that you will always the same object back.
Basically I'm asking if it is actually possible to create a Singleton class
in VBS (via 'proper' techniques, or by tricks/cheats), and if it is possible
am I stepping into a world of hell trying to accomplish it?
Yours,
A Ratcliffe
ar********@archimagic.net
ps. Sorry about the verbiage. Hazard of the trade at times when you are
trying to describe something.