Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Learn Dependency Injection in C#

What is Dependency Injection in C#?
Dependency injection is a design pattern, a technique that helps to inject dependent object of a class without changing the concrete implementation.

Why Dependency injection

So before we learn implementation of Dependency injection we need to understand the concept behind this design pattern, why do we need Dependency injection, and what problem does it solve?

Dependency injection helps implementing object in multitier architecture in such way that when we make changes in one implementation that will have minimum impact on their implementation, thus provide solution to tight coupling implementation

Let's understand the Dependency injection with a real-time simple example.
Note: here in my example I write all my code in place under same namespace, not creating multiple assembly layers, because here I want you to learn "Dependency injection" not three tier architecture .

Business:
We have a SportsCar class, we need to add a new sport car in database, so we have created a method AddCar() in all layer, just assume now we have SQL Database, in three tier architecture scenario how the normal implementation will be done!
Here in example we have created a console app (just to avoid button click in case of web / win app), so we can focus more on Dependency injection using UnityContainer.

Step 1:
First we create data access layer with a simple add method, we have two data access class, one for SQL database and another for Oracle database
public interface IDataAccess
    {
        void AddCar(SportsCar s);
    }
    
    public class MSSQLDal : IDataAccess
    {
        public void AddCar(SportsCar c)
        {
            // database implementation here
        }
    }
    
    public class OracleDal : IDataAccess
    {
        public void AddCar(SportsCar c)
        {
            // database implementation here
        }

    }

Step 2:
Now create a simple SportsCar class with one property and one AddCar() method
public  class SportsCar
    {
        public string CarType { get; set; }
        private IDataAccess dataAccess;
               
        public SportsCar(IDataAccess da)
        {
            dataAccess = da;
        }

        public void AddCar()
        {
            SportsCar c = new SportsCar(new MSSQLDal());           
            dataAccess.AddCar(c);
        }
    }

Step 3:
Now in Program class create a new instance of UnityContainer class.
You may need to add Microsoft.Practices.Unity reference in your project, either can add them manually or use NuGet Package to install and add them in your project.
using Microsoft.Practices.Unity;
public class Program
{
static void Main(string[] args)
{
IUnityContainer iuContainer = new UnityContainer();
iuContainer.RegisterType<SportsCar>();
iuContainer.RegisterType<IDataAccess, MSSQLDal>();
// iuContainer.RegisterType<IDataAccess, OracleDal>();
SportsCar car = iuContainer.Resolve<SportsCar>();
            car.CarType="McLaren 570S";
            car.AddCar();            
        }
    }

Notice how we access the AddCar() method
  • Create a mew instance of UnityContainer
  • Register the object, here SportsCar and both data access classes MSSQLDal, and OracleDal
  • Then get instance of SportsCar
    SportsCar car = iuContainer.Resolve<SportsCar>();
Dependency injection using UnityContainer

Now suppose instead of using SQL Data access layer you want to use Oracle Data Access Layer, you just need to enable the line below
iuContainer.RegisterType<IDataAccess, OracleDal>();
Hope you understood how to use Dependency injection using UnityContainer in C# .Net

You should also read (DI) Dependency Injection & IoC (Inversion of Control)



 
Dependency injection using UnityContainer
Learn C# Online Tutorial


Dependency injection in C#
Group Training