September 22

The Singleton Pattern in .NET – If you can avoid it

2  comments

The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.
NOTE: This pattern is now considered by many (included the creators) an anti pattern and it is presented only for completeness! Read why.
A Singleton is a class that manages a single instance of itself and prevent any other class from creating a new instance on its own. The only way to get the instance is passing through the Singleton class itself. 
 
Singletons are frequently used to provide a global access point for some service.
 
Let’s see an example of Singleton that implement a trivial logging service.
 
 
and an example of usage:
OK but… what are the advantages?
As we said in the definition of the pattern, the main advantages are:
  • Making sure that there is a single instance of the service. This is accomplished making the constructor private and the class sealed.
  • Providing a global point of access to that instance.
Mmm but I can solve the same problem writing the following code, right?
It is true!
However, there are still some advantages in using the Singleton approach:
  • You don’t have to put the static keyword everywhere
  • You can use singletons as parameters or objects
  • You can implement singleton with interfaces
  • You can write tests even if this is not simple
Anyway, singletons have more problems than benefits and these days many developers tend to avoid it if they can.
The biggest problem is the fact that you can use singletons where you want (they are like global variables) and dependencies are hidden inside the client objects. This means that the interface of the client objects don’t reveals all the dependencies and this make testing very hard. In addition, in order to be able to write the tests you have to create a mechanism to replace singletons with mocks (a static Initialize method for example) that in some way violates the definition of singleton itself.
These days there are better ways to solve this problem and they usually involve other patterns like Service Locator and Dependency Injection. I will describe these patterns in later posts.

Tags


You may also like

The best way to stay up to date with C# 10 features

My Technical Journey from my first program to Lead Software Engineer

Top 10 C# Developer News from Microsoft Build 2020

Agile Estimation

  • Thank you very much and thanks for sharing. Very interesting article!

    I didn't mention laziness and thread safety just because I didn't want to put too much emphasis on the pattern that it should be avoided if possible.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Subscribe to our newsletter now!

    Get instant access to the Master C# 9 webinar to learn all the new exciting C# 9 features quickly.
    Get regular videos and news on C# and .NET and offers on products and services to master C#.

    We will collect, use and protect your data in accordance with our Privacy Policy.

    >