Software Engineer
mediumwhat-is-dependency-injection

What is Dependency Injection (DI) and why is it useful?

Answer

Dependency Injection provides a class’s dependencies from the outside instead of creating them inside the class. **Why it matters:** - Improves **testability** (swap real dependencies for mocks). - Reduces **coupling** (depend on interfaces/abstractions). - Improves **maintainability** and configuration. **Common forms:** constructor injection (preferred), setter injection, and framework-based IoC containers. **Interview tip:** Mention DI + DIP (from SOLID) together, and give an example like injecting a `PaymentGateway` interface.

Related Topics

Design PatternsArchitectureTesting