Tuesday, September 9, 2014

Singleton Instance in SimpleInjector

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!!.
You can find complete example of this blog post at following github location. https://github.com/dotnetjalps/SingletonSimpleInjector
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews