Free Web Development Online Tutorials, Learn to Code
Dependency injection using UnityContainer, Learn C# programming Language, what is a dependency injection in c#

Dependency Injection C# Example

Here you learn how to implement Dependency Injection in C# console application example.

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

DI example in C#

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 .

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());           

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<IDataAccess, MSSQLDal>();
    //  iuContainer.RegisterType<IDataAccess, OracleDal>();
    SportsCar car = iuContainer.Resolve<SportsCar>();
    car.CarType="McLaren 570S";

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 C# 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 may be interested in following tutorials:

free C# tutorial online

Dependency injection using UnityContainer
Other Online Tutorial

C# .net Interview Questions Answers

Dependency injection in C#
.Net C# Examples | Join .Net C# Course