I think maybe you're trying too hard to find a reason to use a singleton.
I agree that it's use w/in a web page is kinda pointless because all
the server side things are rebuilt on every trip to the server. And
secondly, saving things to session means you are capturing state. State
means, as I think about it, the specific values of a given object
instance. A singleton is not intended for this kind of use. Unless you
are, on any given trip to the server, creating some number of redundant
objects, I just don't see a need for a singleton.
But here is an example of how we use it. We've created a service - it
runs in the background all the time. This service takes documents and
stores them in our doc archive. The DocImporter is a singleton
instance. Whenever that service is called the singleton ensures that
only one DocImporter object is ever created. This DocImporter object
controls the overall importing process in turn uses the factory pattern
to instantiate any of a number of document type objects, dependent on
the document being imported, of course and one for each document being
imported.
Oh, Additionally, the DocImporter spawns an error logging object when
an error is generated. This ErrorLogger is also a singleton. Errors
generated in the importing process are queued up and wait to be logged
by the one and only ErrorLogger object.
Perhaps the main point here is that the singleton object lives
indefinitely and no client needs it's own specific version of it.
On 2007-01-13 17:32:10 -0600, "fredd00" <fr*************@gmail.comsaid:
Hi
I'm just starting with singleton and would like to implement in my new
web app.
I have a question
lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do
SqlConnection cn = DataHelper.GetConnection()
when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?
What would be the advantage of using singleton objects over putting my
objects in application variables?
Thanks