Inversion of Control (Design Principle) & Dependency Injection (Design Pattern)
Tags: softwareengineering, javascript, node, nestjs
Published: 2024-02-03 10:20:38

## Video Explanation:

Please find the video explanation below:

{% youtube U0LGQjxyuw8 %}

## 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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qjf0k6ok9skfbgt1ma8.png)

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9dn8bdszv70ljhuciy0q.png)

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](https://dev.to/shameel/nestjs-dependency-injection-overview-29h3). I will keep the context for this article.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a52rsvuc0giyn0c2561d.png)

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:
```js
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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5izgo3ksff1wl1q1382.png)


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**:

{% cta https://www.youtube.com/@ShameelUddin123 %} 🚀 Follow on YouTube {% endcta %}

{% cta https://www.linkedin.com/in/shameeluddin/ %} 🚀 Follow on LinkedIn {% endcta %}

{% cta https://github.com/Shameel123 %} 🚀 Follow on GitHub {% endcta %}