Over a million developers have joined DZone.

Singleton Instance in SimpleInjector

· Integration Zone

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

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.

Singleton Class in c#

How we can create Singleton Instance with SimpleInjector:

Here I’m going to create a console application with logger example. So let’s create via creating a new project like below.

singleton-simple-injector

Once we are done with creating project it’s time to add a NugetPackage for simple injector.

simple-injector-nuget-package

Now it’s time to write code. I have created a ILogger interface like below.
namespace SimpleInjectorSingleTonExample
{
    public interface ILogger
    {
        void Log(string message);
    }
}
Now let’s create a simple implementation of above interface with Console.Writeline.
using System;
 
namespace SimpleInjectorSingleTonExample
{
    class Logger : ILogger
    {
        public void Log(string message)
        {
            Console.WriteLine(message);
        }
    }
}
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.
using SimpleInjector;
 
namespace SimpleInjectorSingleTonExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new Container();
            container.RegisterSingle<ILogger,Logger>();
            var logger = container.GetInstance<ILogger>();
            logger.Log("This is a sample Log");
        }
    }
}
It’s contains RegisterSingle method for creating Singleton Method. Now we will learn another way of creating Singleton Instance.
using SimpleInjector;
 
namespace SimpleInjectorSingleTonExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new Container();
            var registartion = Lifestyle.Singleton.CreateRegistration<ILogger,Logger>(container);
            container.AddRegistration(typeof(ILogger),registartion);
            var logger = container.GetInstance<ILogger>();
            logger.Log("This is a sample Log");
        }
    }
}
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.

sample-application-singleton-instance-simple-injector

That’s it. It’s very easy hope you like it. Stay tuned for more!

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

Topics:

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}