I haven't coded anything yet. I'm trying to determine if there is any
advantage to using singletons to improve performance of ASP.NET 2.0
web-apps.
From what I have learned so far, in almost any scenario there is no
advantage, because object instantiation is so cheap in .NET
In fact, there may even be a disadvantage to sharing singleton instances
across multiple page requests, due to multi-threading issues and locking.
This is the area I'm not sure about.
Right now, the area I'm concentrating is processing of a single page request
:
* Over the course of processing a single page request from a single client,
I believe that the advantages/disadvantages of storing class instance(s) in
some sort of cache (and removing them from the cache at end of page
processing) are only relevant if the class object requires a lot of
initialization.
That I know of , there are 4 types of cache the Page can easily use during
in-Page processing: HttpContext.Current.Application ,
HttpContext.Current.Session , or HttpContext.Current.Cache (Page.ViewState
could also be used ... but this would be a mis-use).
Assuming little or not initialization of the class object is required, it
seems that the overhead of retrieving the object from the cache is
approximately the same as the overhead of creating the object fresh.
* What confuses me about singletons in an ASP.NET web-app is the following:
Is it required to cache the singleton object ? True, the singleton has a
internal static reference to a single instance of itself , but, once
instantiated, does this assigned static reference protect the instance from
being disposed and GC'd once all other references to it have been dropped ?
For example: In a class method Class1.Method1() called during Page
processing , I perform dao = singletonDAO.getInstance() , make calls to
dao.method1() , dao.method2(), etc. ... once Class1.Method1() is completed,
is singletonDAO's internal static reference to an instance of itself
disposed (i.e. marked as ready for GC) ?
<tl*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
John, are you storing your instance in HttpContext.Current? Or for
each page are you just doing:
Singleton sg = Singleton.Instance();
Where the Instance method just returns the instance created when the
Singleton class was initialized?
Thanks.
TJ
John A Grandy wrote: For a singleton class utilizes by ASP.NET 2.0 page processing:
When initial instantiation is performed during the initial call to the
retrieve instance method (let's call the method "getInstance()"), an
instantiated object of the class is assigned to the class' internal
static
reference to that an object of itself (let's call the reference
"uniqueInstance")
Does that uniqueInstance have global scope in the context of page
processing
?
In other words, will subsequent calls to getInstance() find
uniqueInstance
!= null , and return it ? Or will they find uniqueInstance == null , and
re-instantiate an object of the class and assign it to unique instance ?