What is Dependency Injection?

Turkish: Dependency Injection

Dependency injection lets a class receive the dependencies it needs from the outside instead of constructing them itself.

What is Dependency Injection?

Dependency injection (DI) is the practice of giving an object the services, repositories, API clients, or configuration it needs instead of letting it create them internally. The class knows what it uses, but it does not need to know how those dependencies are constructed.

For example, if an OrderService receives an email sender through its constructor instead of calling new EmailClient() directly, a test can provide a fake sender without contacting the real email service.

How Does It Work?

DI is commonly applied through constructor injection, method injection, or property injection. Constructor injection is often preferred because required dependencies are visible at creation time and are harder to leave unset.

Frameworks with DI containers define the dependency graph, create the required objects, and pass them into classes with the right lifecycle. Spring, .NET, NestJS, and Angular all use this approach widely.

Why Is It Used?

Dependency injection improves testability, loose coupling, and replaceability. If every small class is abstracted unnecessarily, however, the code can become harder to read.

As a design pattern, DI is closely related to the Dependency Inversion idea in SOLID. When business rules do not depend directly on concrete infrastructure classes, changes to a database, email provider, or external API have a smaller blast radius.