← All Quizzes
What is Dependency Injection and what benefits does it provide?
Explanation
Dependency Injection (DI) is a technique where an object's dependencies are provided externally rather than created internally. This implements the Dependency Inversion Principle. Types: constructor injection (preferred), setter injection, and field injection. Benefits: loose coupling, easy testing (inject mocks), reusability, and configuration flexibility. DI containers (Spring, NestJS, .NET) automate dependency resolution and lifecycle management. DI makes dependencies explicit in constructors, improving code clarity. It is fundamental to modern framework architecture and testable code.