Inversion of Control (Design Principle) & Dependency Injection (Design Pattern)
Shameel Uddin
Posted on February 3, 2024
Video Explanation:
Please find the video explanation below:
Design Principle and Design Pattern
Before diving deep into Inversion of Control and Dependency Injection, let us first discuss about design principle and design pattern
*Design principle give you a general idea or a theoretical solution of a certain problem whereas, design pattern provides implementation details for that particular solution.
*
Design Principle - Encapsulation
Encapsulation suggests bundling the internal workings of a system and exposing only the necessary interfaces.
In the case of a vehicle, engine, transmission and other components are encapsulated. Only external interfaces like accelerator, brake, and steering wheel are exposed for user interaction.
This protects the internal complexity and allows for easier maintenance and upgrades.
Design Principle - Builder Pattern
Builder Pattern is one of the implementation technique of encapsulation which defines a step by step process of how you can create the vehicle.
Conclusion
Overall, I can say that, design principle in this case guides overall design of the vehicle whereas, design principle takes into its hands the entire manufacturing process.
Inversion of Control and Dependency Injection
Inversion of Control is Design Principle and Dependency Injection is a Design Pattern
Understanding Dependency
Following is the code of the Engine:
A car depends upon the engine, otherwise it won't work. So, I can say that the engine is the dependency of the car.
That means, engine needs to be fit inside the car. Or, I can say that, the car has to create the object of Engine inside of it.
Engine is just one component of a car, however, car can have a lot of components like mirrors, doors and etc. Each and every single component is referred to as a dependency.
In the example I showed just above, the entire dependency management is done by our code.
Management of dependency injection can become more complex as more dependency grows along with their relationships with one another. For example, window has a relationship with a door, transmission has relationship with engine and so on.
Inversion of Control
IoC (Inversion of Control) suggests a theoretical idea, as such, the flow of control should be reversed (inverted).
What does that mean?
It means this:
Library, framework or a run-time should take responsibility of handling and managing all the dependencies instead of it being handled/managed by the developer's code.
Since we are following Nest.js series then that means, nest.js run time system will handle dependency management. It will have its own IoC container where all the resolved dependencies would be available (after they are successfully resolved).
Dependency Injection
It is a design pattern which takes the responsibility to implement the theoretical solution provided by Inversion of Control design principle.
I explained it in detail here. I will keep the context for this article.
What happens here is that, when the nest.js server starts, it resolves all the dependencies (decorated by @Injectable()
) decorator and keep them in its IoC Container.
Whenever a class intends to inject the dependency like this:
constructor(private readonly catService:CatService)
then nest.js runtime looks through its IoC container, find the dependency and inject it into the class that is asking for it.
Nest.js resolves all the dependencies that are registered in a module in providers
array:
As you can see, dependency injection allows the creation of the objects outside of the class and has a way to present these objects to the dependent class through various ways. The way that we saw earlier is through constructor injection.
Conclusion
The article explained design principle and design pattern with an analogy example and further explained Inversion of Control and Dependency Injection in the same context.
Happy coding! 🚀
Follow me for more such content:
Posted on February 3, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
February 3, 2024