I have inherited a Business Object architecture that makes heavy use
of the Singleton design patter. For example the data access layer
class is implemented as a static Singleton "object", there are also
other business objects logic and security supporting classes that are
all implemented using the Singleton patter. I 'm attempting to make
use of this class library in a webservice application. The problem is
that some of the Singleton objects use static variables to store data
and that will cause problems in the webservice environment where all
static variables are equivalent to application level variables. I've
done some research and found an alternate way of instantiating these
Singleton objects.
I would like to get your opinion as to how reliable this method will
be (when used in the webservice and a single threaded windows app) and
any potential pitfalls. Looks like this type of Singleton
instantiation will guerantee that each web app thread will have its
own static variables.
TIA for your time.
(Found at http://ehsanbraindump.blogspot.com/2...endment-1.html)
public class ConnectionManager
{
/* ConnectionManager implementation */
//Singleton logic:
private ConnectionManager() { }
public static ConnectionManager Instance
{
get
{
if (Thread.GetData(Thread.GetNamedDataSlot("Connectio nManager")) ==
null)
Thread.SetData(Thread.GetNamedDataSlot("Connection Manager"), new
ConnectionManager());
return
(ConnectionManager)System.Threading.Thread.GetData (Thread.GetNamedDataSlot("ConnectionManager"));
}
}
}
Rich