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
Theory
Definition of each word
"Patron de design"
- Patrón: this is a type of events or repetitive objects
- 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
- Dependency: refers to a relationship in which one thing needs another to function properly
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.
Practice
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.
Method: without dependency injection
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.
Method: dependency injection
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.
Top comments (0)