It is more difficult to create synchronized shared class than multiple
instance classes, and thus should be generally avoided.
There are reasons for singletons (or static classes. They are the same), but
quite limited. But they are design decisions, not a runtime matter.
Singleton or static does not mean only one thread at a time can execute the
method. If you want that, you need to synchronize the accesses (look for
lock keyword).
If you are programming for multiple threads, you need to be more concerned
with thread synchronization (shared data) than singletons or static
classes/methods.
For example, your function
public DataSet GetCustomer()
{
return runProcedure("sp_Customers");
}
could be (I don't know what runProcedure does) thread safe, since it does
not access any shared (public) data.
If also runProcedure() does not access any class level variables/data, you
can call this function from any number of threads you like.
And from as many instances you like. If the class in which the function
belongs does not have instance data (i.e. has no shared data), it makes sens
to make it a static class, because there would not be any instance data
making an instance quite useless.
"PokerMan" <no****@pokercat.co.ukha scritto nel messaggio
news:Ol**************@TK2MSFTNGP02.phx.gbl...
Hi
Ok in my app i have some classes that will only ever have one instance.
All of which i make Singletons.
I also have another class that acts like an easy access wrapper for any db
access. Again this is a one time instance.
If it wasn't a one only instance is there a danger in db access? Such as
two threads calling the same method and updating something incorrectly?
Should i make that class a singleton, or make all the methods in that
class static? Since a singleton returns a static instance, does that not
do the same as setting all methods to static?
To be clear an example of one method is like this:
public DataSet GetCustomer()
{
return runProcedure("sp_Customers");
}
Thanks in advance