This article assumes a basic understanding of the de-coupled architecture and Inversion of Control (IoC) principal. If you are very new in both concepts I recommend you understand both first. We all know that the de-coupled architecture is one of the major goals of a good design pattern in applications and Inversion of Control is the way to do that. As the name Inversion of control suggests, we will redirect the control follow in a reverse order to implement de-coupled architecture.
There are the following two implementations of Inversion of Control:
- Dependency Injection
- Service locator.
In this example we will implement Dependency Injection using Unity within a MVC5 application. So, to follow this article I suggest you create an Empty MVC 5 application and install the following packages from the NuGet Package Manager.
Please note that Unity is a product from Microsoft and it will help us to create a repository of dependency classes that will be pluggable in mode. In other words, the main advantage of Unity is that if needed we can just plug it in with another application.
Use the following procedure to install the following packages:
Once installed successfully you will find that the following assembly has been added to the application.
Step 1: Create Unity container
Fine, we will now add a Bootstrapper class to the application. Take one .cs file and modify it accordingly.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using DITest.Repository;
- using Microsoft.Practices.Unity;
- using Microsoft.Practices.Unity.Mvc;
- namespace DITest
- {
- public class Bootstrapper
- {
- public static IUnityContainer Initialise()
- {
- var container = BuildUnityContainer();
- DependencyResolver.SetResolver(new UnityDependencyResolver(container));
- return container;
- }
- private static IUnityContainer BuildUnityContainer()
- {
- var container = new UnityContainer();
- // register all your components with the container here
- //This is the important line to edit
- container.RegisterType<ICompanyRepository, CompanyRepository>();
- RegisterTypes(container);
- return container;
- }
- public static void RegisterTypes(IUnityContainer container)
- {
- }
- }
- }
- container.RegisterType<ICompanyRepository, CompanyRepository>();
Step 2 : Register container class
We will now register the container class in the global.aspx page. Here is the screen of my global.aspx page.

This single highlighted line will register the Unity container in the application.
Step 3 : Create class /Model
We will now create the model/class. In this example I have decided to implement one “company” class, that will hold a few eices of company information. The definition is as in the following.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace DITest.Repository
- {
- public class Company
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Category { get; set; }
- }
- }
I recommend you create a folder called “Repository” in the solution and keep the model in that place, because in the future we will add two more classes to deal with the mode.
Step 4 : Create Interface
Create one IProductRepository.cs file in the “Repository” folder and modify the code accordingly as in the following:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace DITest.Repository
- {
- public interface ICompanyRepository
- {
- IEnumerable<Company> GetAll();
- Company Get(int id);
- Company Add(Company item);
- bool Update(Company item);
- bool Delete(int id);
- }
- }


Sasi RekhaPosted Jul 21, 2017, 1:13 AM
Thank you so much .It is great article
aetheriusPosted Mar 23, 2017, 1:10 PM
Hello is this possible to use an existing project inside main project as plugin injected by ioc? is there any sample project for that
Siva VPosted Jan 12, 2016, 4:17 AM
if there is code download availblity it will helps for beginners
SarangaPosted Apr 23, 2015, 2:12 AM
Thanks Sourav for this article, recently I have created blog post about Unity.Mvc5 Library. http://sarangasl.blogspot.com/2015/04/mvc-5-with-unity-for-dependency.html
Mike ColePosted Sep 2, 2014, 11:31 PM
Why not just use the built-in PerRequestLifetimeManager? http://stackoverflow.com/a/20698304/86191
Sourav KayalPosted Jun 2, 2014, 8:20 AM
Thanks Sandeep. Ninject article too coming very soon.
Sandeep Singh ShekhawatPosted Jun 1, 2014, 2:22 PM
Good one! I will try it. I am using Ninject and Autofac...