
How to implement Singleton pattern
Markus,
This might not be possible with your setup, but you might want to
consider COM+ and a serviced component for what you want to do. Using COM+,
you can create an object pool for your object. By setting the pool minimum
to 1 and the maximum to 1, you can effectively create a singleton. Also,
using COM+, you can manage concurrency (activity and causalities).
Hope this helps.
--
- Nicholas Paldino [.NET MVP]
Quote:
> I found two ways to implement the singleton pattern.
> 1)
> sealed class Singleton1
> {
> private Singleton1();
> public static readonly Singleton Instance = new Singleton1();
> public void MyMethod()
> {
> //doSomething
> }
> }
> 2)
> sealed class Singleton2
> {
> static Singleton2();
> public void MyMethod()
> {
> //doSomething
> }
> }
> using it looks like
> {
> Singleton1.Instance.MyMethod();
> Singleton2.MyMethod();
> }
> I'm not sure if the second way has the same thread safety as the first
one.
> Does anybody see a difference (except the calling) between the two way's.
> Which is the better one?
> tnx for your opinion
> Markus