All you Need to Know about Dependency Injection
Check how this widely used design pattern works behind the scenes
Dependency injection is a design pattern in object-oriented programming that allows you to inject objects (also called dependencies), into a class or function from an external source, rather than creating them internally within the class or function. The external source could be a configuration file, another object, or a framework.
Why should you use DI
The main benefit of dependency injection is that it enables loose coupling between the objects in a system. Loose coupling is a design principle that aims to reduce the interdependence between components of a system. When objects are loosely coupled, changes to one object do not require changes to other objects. It reduces drastically the complexity and improves the maintainability of your application.
Dependency injection works by separating the responsibility of creating objects from the class or function that requires them. This separation is achieved through the use of interfaces, which are contracts that define the methods and properties that a class must implement. The class that requires the dependency depends only on the interface, not on the concrete implementation.