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().
See 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 the following snapshot.
How to configure the 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 the 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 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 the following snapshot it is called the DA (Data Access) layer.
Final output
It has finally injected dependency.
Dependency Injection Pros and Cons
- Pros
- Loosely coupling.
- Increase Testability.
- Cons
- Increase code complexity.
- Complicated debugging of code.
The preceding is a simple example for developers. After this, you will find Dependency Injection to be an easy concept.