Suggest an editImprove this articleRefine the answer for “How does DI differ from ordinary manual dependency injection?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The difference between **Dependency Injection (DI)** and **manual dependency injection** is in who controls the process of creating and wiring objects together. Both approaches *inject* dependencies, but they do it differently. **Key point:** manual injection is simply "passing objects around by hand", while Dependency Injection is an automated dependency-management system, where you describe *what is needed*, and the container decides *how and when to create it*.Shown above the full answer for quick recall.Answer (EN)ImageThe difference between **Dependency Injection (DI)** and **manual dependency injection** is in ***who controls the process of creating and wiring objects together***. Both approaches *inject* dependencies, but they do it differently: --- ### 1. Manual dependency injection: control belongs to the programmer You create the objects yourself and pass them into other classes. ```java Database db = new MySQLDatabase(); UserService userService = new UserService(db); ``` **Pros:** - Simple, transparent, no extra magic. - Fits small systems. **Cons:** - The programmer manually manages the dependencies and their lifecycle. - As the project grows, the initialization code turns into a "ladder of constructors": ```javascript A depends on B, B depends on C, C depends on D... ``` - Replacing an implementation requires rewriting the creation points. - Hard to scale and test: everything is tightly coupled. --- ### 2. Dependency Injection (through an IoC container): control belongs to the framework A DI container (for example, **Spring**, **Guice**, **.NET Core**) itself: - creates the needed objects, - resolves dependencies, - manages their lifecycle (singleton, prototype, etc.), - and "injects" them wherever they are needed. ```java @Component class UserService { private final Database db; public UserService(Database db) { this.db = db; } } ``` The container finds the right `Database` implementation itself and plugs it into `UserService`. **Pros:** - The architecture becomes **flexible and modular**. - **Implementations are easy to swap** without rewriting code. - **Lifecycle management** - the container decides when to create and destroy objects. - **Simplified testing** - a mock can be plugged in through configuration. **Cons:** - Requires learning the container. - Harder to debug the "hidden magic" if you do not know how the framework works. --- ### Overall comparison | Criterion | Manual injection | Dependency Injection | |---|---|---| | Who creates dependencies | The programmer | The container (framework) | | Scalability | Low | High | | Flexibility | Limited | High | | Lifecycle management | Manual | Automatic | | Testing | Harder | Easier | | Best fit | Small projects | Medium and large systems | --- **Conclusion:** > Manual injection is simply "passing objects around by hand". > Dependency Injection is an **automated dependency-management system**, where you describe *what is needed*, and the container decides *how and when to create it*. If you want, I can show a short code example of what the same project looks like **before** and **after DI** (in Java or Python)?For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.