Before some time I have written a blog post Dependency Injector with Simple Injector. This is a second blog post in same series. In this blog post we are going to learn How we can create a singleton Instance in Simple Injector.

What is is Singleton:

This is one of the Gang of Four creational pattern. Idea of behind the singleton pattern is to have only one instance of class of at anytime. This pattern ensures that a class will has only one instance of it and provides a global point of access to that instance.

I have already written a global blog post about that. You can find a basic example of Singleton pattern in C# from the following link.

So we are done with our implementation of Logger it’s time to write a code for creating a Singleton Instance with help of SimpleInjector DI framework. There are two ways you create a Singleton and I’m going to show you both. First is very easy like following.

Here we have created a registration with Singleton life style and then used that to get Singleton Instance of logger class. Now when you run any of way for creating singleton instance application it will get following output.