Let's have a look at where it starts. It begins with the SOLID principle last Letter “D”, the Dependency Inversion Principle.
Agenda
- Dependency Inversion Principle.
- Inversion of Control.
- Dependency Injection.
- Creating a console application.
- Adding Reference to Microsoft Unity Framework.
- Adding BL class.
- Adding DL class.
- Adding Interface.
- How to configure the Unity Container.
- Running and debugging an application.
- Final output.
- Dependency Injection Pros and Cons.
Flow of Dependency Inversion Principal

Dependency Inversion Principal
The Dependency Inversion principle says that components that depend on each other should interact via an abstraction, not directly with a concrete implementation.
Example
If we have a data access layer and a business layer then they should not directly depend on each other, they should depend on an interface or abstract for object creation.
Advantages
- Using abstraction allows various components to be developed and changed independently of each other.
- And easy for testing components.
Inversion of Control (IOC)
Inversion of Control is a design principle that promotes loosely coupled layers, components, and classes by inverting the control flow of the application.
Dependency Injection (DI)
- Dependency Injection is defined as a design pattern that allows for removing hard-coded dependencies from an application.
- There is one major point to remember.
- “Inversion of control is principal and Dependency Injection is implementation”
- Now let's start with implementing Dependency Injection using the Microsoft Unity Framework.
The Microsoft Unity Framework helps us to inject external dependencies into software components. To use it in a project we just need to add a reference for the Unity Container DLLs to our project. To download it click on this link.
There are the following three types of dependencies.
- Constructor
- Setter (Properties)
- Method.
Creating a console application
I am creating a Console application named DIinject.
I am showing Constructor Injection with Unity Framework (which is the most widely used).
See the following snapshot.

Adding Reference to Microsoft Unity Framework
Now I have added a reference for the Microsoft Unity Framework to my application.
See the following snapshot.

Adding BL class
I have added BL.cs (a Business Logic Layer) class to the application.
See the following snapshot.

Adding DL class
After adding BL.cs I have added the DL class to the application (Data Access Layer).
See the following snapshot.

Adding Interface
For decoupling both classes, I have added an interface with the name IProduct.
See the following snapshot.

In the interface, I have declared a method with the name Insertdata().














Rakesh reddyPosted Jun 22, 2018, 9:37 PM
Nice explanation
jaimini thakkerPosted Sep 19, 2017, 10:30 AM
Simple and sweet example. Very helpful
Josh YatesPosted Oct 20, 2015, 3:07 PM
I started using Ninject, but have switched to Unity. It also help when I tell management I'm using Microsoft Unity....Ninject raises questions just because of the name. haha. But, when I tell them Microsoft Unity, they give me that ...good...good..Keep using Microsoft look.
Prakash JoshiPosted Feb 12, 2015, 3:42 AM
Nice article to work with Unity as DI and good to quote “Inversion of control is principal and Dependency Injection is implementation”. Thanks Saineshwar Bageri sir
Saineshwar BageriPosted Feb 4, 2015, 6:58 AM
thanks Vithal sir
Vithal WadjePosted Feb 3, 2015, 9:47 AM
nice
Pramod LawatePosted Feb 3, 2015, 7:36 AM
very good
Arweb AroshanzamirPosted Feb 2, 2015, 11:57 PM
one nice.. thanks sir...