Groups | Blog | Home
all groups > c# > april 2007 >

c# : understanding Singleton


ruthie
4/9/2007 11:43:22 PM
Hi,

I am a bit confused using Singleton pattern.
Using Singleton - do I have one instanse of the class for each web
client or do I have one instanse for all users ?

Thanks.

Ruthie.
Giulio Petrucci
4/10/2007 12:00:00 AM
ruthie ha scritto:
[quoted text, click to view]

The main feature of the Singleton design pattern is to have a single
instance of a class for each AppDomain (correct?).

Bye,
Giulio

Jon Skeet [C# MVP]
4/10/2007 12:05:00 AM
[quoted text, click to view]

It's not entirely clear what you mean. Are you writing an ASP.NET
application, or are you writing a web client?

If you're writing an ASP.NET application, there will be one for the
whole web application - but you should be aware that ASP.NET can
recycle its applications periodically, so the old one may be thrown
away and a new one created.

If you're writing a web *client*, there will be one for each process.
(More strictly speaking, for each AppDomain - but that distinction is
unlikely to be an issue.)

Jon
ruthie
4/10/2007 12:54:34 AM
[quoted text, click to view]

Thanks Jon & Giulio ,

You answered my question - I am developing an asp.net application
project.

Best Regards,
Ruthie.
Everest
4/11/2007 2:30:38 AM
[quoted text, click to view]

Hi Ruthie,

Answer to your question is: You will have One instance for all users.

Singleton pattern restricts instantiation of a class to one and only
object. hence all the requests will be served by the same instance of
class.

Check out the link: http://en.wikipedia.org/wiki/Singleton_pattern

Regards
Arne_Vajhøj
4/11/2007 8:40:56 PM
[quoted text, click to view]

Which raises the question: what would be the best way
to get some code executed when that happen to the object ?

Finalize ?

Jon Skeet [C# MVP]
4/12/2007 12:00:00 AM
[quoted text, click to view]

I'd hook into the AppDomain.DomainUnload event, I think. I'd also try=20
to design things so that it wasn't necessary :)

--=20
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button