Inversion Of Control (IoC) Introduction
The terms Inversion of Control (IoC), While Dependency Injection Principle (DIP), Dependency Injection (DI), and IoC containers are also acquainted. however are you clear regarding what every term means
Inversion Of Control (IoC)
In this chapter, we are going to find out about IoC and the way to implement it. While This may be the primary step towards achieving loosely coupled style as illustrated by the following figure.
Dependency Inversion Principle (DIP)
In the previous chapter, we tend to learn concerning implementing IoC principle employing an industrial plant pattern and achieved the primary level of loosely coupled style. While Here, we are going to learn and implement the Dependency Inversion Principle because the second step to realize loosely coupled classes.
First of all, let’s perceive what’s Dependency Inversion Principle (DIP)?
Dependency Inversion Principle is one amongst the SOLID object orientating principle fabricated by parliamentarian Martin (a.k.a. Uncle Bob)
Dependency Inversion Principle Definition
- High-level modules shouldn’t rely upon low-level modules. Each ought to rely upon abstraction.
- Abstractions shouldn’t rely upon details. Details ought soon to rely upon abstractions.
What is Abstraction Dependency Inversion Principle
Abstraction and encapsulation area unit necessary principles of object-oriented programming. There area unit many alternative definitions from many of us.
Dependency Injection (DI) may be a style pattern used to implement IoC while wherever it permits the creation of dependent objects outside of a category and provides those objects to a category in numerous ways that. While using DI, we have a tendency to move the while creation and binding of the dependent objects outside of the category that depends thereon.
Dependency Injection pattern involves three forms of categories.
Client Class: The consumer category (dependent category) may be while a category that depends on the service class service Class: The service category (dependency) may be a category that has service to the consumer category.
Injector Class: The convenience category injects service category object into the consumer category
Types of Dependency Injection
Constructor Injection: While In the constructor injection, injector supplies service (dependency) through the client class constructor.
Property Injection: In property injection (aka Setter Injection), injector supplies dependency while through a public property of the client class.
Method Injection: while In this type of injection, client class implements an interface which declares the method(s) to supply while dependency and the injector uses this interface to supply dependency to the client class.