Dependency Injection using Unity container C#

Hi,

It is really cloudy day in Thane , INDIA and i am waiting for rain so the Ragi crop cultivated by my father and uncle at my native place get sufficient water to grow.

OK. what is Dependency Injection using Unity container C#? but before that imagine there is no Dependency Injection concept present in C#.

Life Without Dependency Injection:

Suppose you have class A depends on class B. without Dependency Injection, these two classes are tightly bounded(coupled) and change in either one of class resulted in error in another.

so with Dependency Injection (DI) using Unity container, we have following advantage(s).

1.Dependency injection creates a lose coupling
between the classes as changes in one class does not impact the other dependent classes.

2.Dependency injection injects the dependencies of a class at runtime.

Adding Unity in our application

To implement the dependency injection using the Unity DI container, we add the Nuget package for Unity which adds the required references to the project.

unity-dll-e1499782417388.jpg

Adding the above Nuget package adds the following references to the project:

unity-dlls.png

Lets consider following sample program,

using Microsoft.Practices.Unity;
using System;
 
namespace UnityContainerTest
{
    class Program
    {
 
        static void Main(string[] args)
        {
            // Declare a Unity Container
            var unityContainer = new UnityContainer();//Line 1
 
            //Dependency Injection(DI) container
            unityContainer.RegisterType<IMainInterface, MainClass>();//Line 2
 
            //Inject the dependency at run time
            MainClass Unity = unityContainer.Resolve<MainClass>();//Line 3
            string Msg = Unity.ReturnMsg();//Line 4
        }
    }
    public interface IMainInterface
    {
        string ReturnMsg();
    }
 
    public class MainClass : IMainInterface
    {
        public string ReturnMsg()
        {
            return "Welcome to Unty Container World!";
        }
 
    }
}

 

Lets decode above sample program,

1.To create loosely coupled architecture using dependency injection, we use an interface IMainInterface which removes the direct dependencies between the classes i.e. Class Program and Class MainClass.

2.First we instantiate the container (Line 1).

3.We register dependencies using the RegisterType method (Line 2).

4.Line 2 adds a mapping between the IMainInterface and MainClass types.

5. This means that whenever a dependency of type IMainInterface is required, an instance of MainClass is created and is injected into the dependent type (Line 3).

6. we use the resolve method, so necessary dependencies are automatically injected (Again Line 3) at Runtime. By doing that, we are not concerned with providing the dependencies ourselves.We can get an object of MainClass class using the Resolve().

7.Finally, we use method ReturnMsg of class MainClass in class Program using dependency injection (Line 4). Again please note that, class MainClass and class Program not directly coupled and they are communicated via. interface IMainInterface.

Thanks.

Any suggestion truly appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s