Dependency Injection | Complete Explanation (Definition, usage, structure and example)
Tags: designpatterns
Published: 2023-06-26 23:33:17

Dependency Injection | Complete Explanation (Definition, usage, structure and example)

## Introduction

Hello buenas, my name is Daniela Barazarte and I want to welcome you to this complete explanation about adding dependencies. This explanation will be intuitive and simple as well as all the explanations that form part of #DetectaLaLogica.

If you prefer the videos, here you have a complete tutorial made by me on YouTube, it is in Spanish but also has subtitles: https://www.youtube.com/watch?v=Sz4acLjFvrU

## <u>Theory</u>
### Definition of each word
**"Patron de design"**
- **Patrón**: this is a type of events or repetitive objects
![Design pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8hizh9zs3bzgaimxs1n.png)
- **Design**: creative activity that aims to design objects that are useful and aesthetic

**"Injection of dependencies"**
- **Inyección**: refers to the act of introducing something into an element

![Inyectadora](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3azdpqk4qt2u5vxxq9pt.png)
- **Dependency**: refers to a relationship in which one thing needs another to function properly

![Relationship](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxaoda69sthrb99v7zkl.png)


### Full definition
The dependency injection is a software design pattern that consists of providing an object with all the dependencies it needs to function, instead of this object creating them by itself.

### #DetectaLaLógica
Dependency injection is called that because you are injecting components from one object to another and that relationship is a dependency because without this injection, the object could not work properly.

## <u>Practice</u>
### Exercise
You must make a console project that generates a ticket to enter a football game.

You can do it in many ways, but we will try the method of doing it with the Inyección de Dependencies.


**<u>Method: without dependency injection
</u>**


![Sin inyección](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ymejw9a7oz4bvrvhh0v.png)

The methods, views, classes, code and other components of the tickets would be disordered in the code and would join with the code of the football stadium.

**<u>Method: dependency injection
</u>**


![Con inyección](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/md7f5rf8cnbdol35i5p4.png)


The methods, views, classes, code and other components of the tickets are fully sorted and separated from the football stadium code. Ticket le passes its components to the stadium by means of the addition of room (this room is painted in golden color).

## Importance
**El patrón de diseño inyección de dependencia has several benefits:**

- **Greater code reuse**: if you want to reuse the ticket component and place it in another project, for example, a concert could do it
- **Greater flexibility**: allows components to be changed easily without affecting the rest of the application.
- **Divide the code into smaller and clearer parts**: what makes the code easier to understand and maintain.


## Farewell

(Recuerda) #DetectaLaLógica: Dependency injection is called that because you are injecting components from one object to another and that relationship is a dependency because without this injection, the object could not work properly.

You can practice this theme in my GitHub repository (Code in C# language): https://github.com/danielabarazarte/DetectaLaLogica/tree/main/Conceptos

Thank you so much for reading, if you have any questions you can comment on it and you can also follow me to see more posts of this style, thanks <3.