Some of what I've been reading about this yesterday and today:
- (SO) Should you avoid static classes?
- (SO) When to Use Static Classes in C# - Mark S. Rasmussen makes a nice comment here
- Singleton vs Static - Nice quote from here:
These differences are extremely subtle but they do impact how resilient the system will hold up after years of subtle changes, new features, enhancements, etc. Most developers dislike system maintenance and I feel the biggest reason is because systems aren't really designed to be maintained. By incorporating the "singleton" pattern above, I've abstracted object creation and typing away from my core business logic which is critical to long-term maintenance.
- Singleton Considered Stupid
- (SO) Class with single method — best approach? - Once again, a quote from Mark S. Rasmussen:
Demanding consumers to create an instance of classes for no reason; True utility classes that do not pose any risk to bloat are excellent cases for static methods - System.Convert as an example. If your project is a one-off with no requirements for future maintenance, the overall architecture really isn't very important - static or non static, doesn't really matter - development speed does, however.
- (SO) Making Methods All Static in Class - Speed is not an issue right now for me, the code must work *PROPERLY* when called by multiple clients from the website and from (possibly, but unlikely) multiple clients from the admin app.
We are (I am) busy rewriting of of our main applications at the moment, and one of the projects in the solution is "Common" and it contains a single class, "clsCommon". This class has no properties, a single private method that is called as the only line in the constructor and then just public methods.
This class handles things like (not the real method names or signatures):
- Copying files:
- CopyFile
- DecryptAndCopyFile
- XML Settings File:
- GetSpecificXMLValue (queries an XML settings file for a value)
- SetSpecificXMLValue
- DeleteSpecificXMLValue
- Creating directories
- Getting timestamps as strings (`return DateTime.Now.ToString("yyyyMMddHHmmss");`)
- Various string functions on passed parameters
- Writing out to log files.
- Checking a passed string parameter if it contains only elements in a whitelist (the whitelist is `readonly` private List that is assigned a value in the constructor.)
Currently all the other projects in the solution have a reference to this project, and they provide access to the class by:
namespace Us.OtherProjects
{
public class clsCommon : Us.Common.clsCommon
{}
}
namespace Us.OtherProjects
{
public static class clsMain
{
public static clsCommon CommonClsClassReferenceForGlobalUsage = new clsCommon();
.
.
.
}
}
namespace Us.OtherProjects
{
public class clsOtherClass
{
.
.
.
private void someMethod()
{
string something = clsMain.CommonClsClassReferenceForGlobalUsage.Foo();
}
.
.
.
}
}
What are your opinions about making this class a static class, or maybe a singleton as described by Jon Skeet here?