In my win forms application I have a single Service class called DataServices
which gives data base access services for all other client classes (about 12 classes) when it comes to CRUD
operations. (i'm using ADO.net). This class has many public methods. Following is a part of the interface implemented by this class.
interface IDataService
{
BankAccount GetByACNo(string acNo);
bool InsertAccount(IBankAccount ba);
bool UpdateAccount(IBankAccount ba);
bool InsertClient(IClient newClient);
Client GetClientByCID(int CID);
Client GetClientByName(string clientName);
void UpdateClient(IClient changedClient);
DataTable LoadClientNamesAndCID();
DataTable LoadPointNamesAndPID();
bool InsertPoint(IPoint newPoint);
Point GetPointByPID(string id);
bool UpdatePoint(IPoint point);
Point GetPointByName(string id);
List<string> GetPIDs(string firstLetterOfPointName);
// other methods...
}
Since these methods are not using any instance variables, is it better to make all methods static
?
From MSDN http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx
"Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity."
Questions
1) Well this depends on how you are really calling your methods, you can use static methods if you do not have any reason to create an instance of the class. There is nothing wrong with this approach.
2) Not a problem if the static methods are setup correctly. Each user whether the method is static or not will have the same affect on the underlying datasource (XML, DB etc)
There's a difference in general design though - static methods should almost always be thread-safe (i.e. you should make them thread-safe) whereas instance methods don't generally have to be (although you should document your class's thread-safety).
hope this helps