Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Singleton Instance in SimpleInjector

DZone's Guide to

Singleton Instance in SimpleInjector

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

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!

Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}