Let's have look at where it starts. It begins with the SOLID principle last Letter “D”, the Dependency Inversion Principal.
Agenda
- Dependency Inversion Principal.
- 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 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 principal 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 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 principal 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 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.
And I am showing Constructor Injection with Unity Framework (that is mostly widely used).
See in the following snapshot.
Adding Reference to Microsoft Unity Framework
Now I had added a reference for the Microsoft Unity Framework to my application.
See in the following snapshot.
Adding BL class
I have added BL.cs (a Business Logic Layer) class to the application.
See in the following snapshot.
Adding DL class
After adding BL.cs I have added DL class to the application (Data Access Layer).
See in the following snapshot.
Adding Interface
For decoupling both classes, I have added an interface with the name IProduct.
See in the following snapshot.
In the interface, I have declared a method with the name Insertdata().
See in the following snapshot.
Now the DL (Data access layer) will implement the interface IProduct.
See it in the following snapshot.
After implementing the interface, now in the BL (Business Logic) layer I am injecting a dependency from the Constructor.
See in the following snapshot.
How to configure Unity Container
In the Program.cs add the following code to register the dependency and resolve the BL (Business Logic) layer instance as shown below. This will automatically take care of injecting the DA (Data Access) layer object into the BL (Business Logic) layer Constructor.
For that, we need to register a type and resolve an instance of the default requested type from the container.
- Creating a Unity Container.
Here I have created a Unity Container object with a named IU.
- Register a type.
Here I registered types that I am using for the injection.
In this, I am injecting a DA (Data Access) layer into the BL (Business Logic) layer. That is why I registered both types.
- Register a type with specific members to be injected.
- RegisterType<From , To>( );
From: Type that will be requested.
To: Type that will actually be returned.
You call the RegisterType method to specify the registered type as an interface or object type and the target type you want to be returned in response to a query for that type. The target type must implement the interface, or inherit from the class, that you specify as the registered type.
- Resolve. (Resolve an instance of the default requested type from the container.)
We want to resolve the BL by injecting a DL. That is why I wrote Resolve<BL>( );.
- Finally calling the method.
See in the following snapshot containing the entire Program.cs.
Running and debugging application
Now just run the application.
I show below a snapshot of running the BL during debugging that clearly shows the DL injecting.
See in the following snapshot the Data Access layer injecting using the constructor.
See in the following snapshot a method of BL has been called.
See in the following snapshot, it is calling a method of a DA (Data Access) layer.
See in the following snapshot it has called the DA (Data Access) layer.
Final output
It has finally injected the dependency.
Dependency Injection Pros and Cons
Pros
- Loosely coupling.
- Increase Testability.
Cons
- Increase code complexity.
- Complicate debugging of code.
The preceding is a simple example for developers. After this, you will find Dependency Injection to be an easy concept.