Dependency Injection
Noun · Development
Definitions
Dependency Injection is a design pattern where an object receives the other objects it depends on from an external source rather than creating them internally. Instead of a class constructing its own database connection or logger, those dependencies are passed in through the constructor, a setter method, or an interface. This promotes loose coupling, making components easier to test (you can inject mock dependencies), swap out (changing from MySQL to PostgreSQL), and reason about. DI is a core principle in frameworks like Spring (Java), Angular (TypeScript), and ASP.NET Core. DI containers or IoC (Inversion of Control) containers automate the wiring by resolving and injecting dependencies at runtime based on configuration or annotations.
In plain English: Rather than a piece of code going out and grabbing what it needs, you hand it everything it needs from the outside, like pre-packing a lunchbox instead of sending someone to the store.
Example: "Instead of newing up the database client inside the service, we inject it through the constructor — makes mocking trivial."