What problem does dependency injection (DI) solve?
DI: instead of a class instantiating its collaborators with new, they are supplied from outside. This decouples construction from use, easing testing and swapping implementations.
2 / 5
Which is the most testable form of dependency injection?
Constructor injection: dependencies are passed in via the constructor, making them explicit and mandatory. In tests you simply pass mocks or fakes, with no hidden global state to set up.
3 / 5
How does the Dependency Inversion Principle relate to DI?
Dependency Inversion: code should depend on abstractions (interfaces), not concrete classes. DI is the mechanism that supplies a concrete implementation of that abstraction at runtime.
4 / 5
Why is the service locator pattern often considered an anti-pattern compared to DI?
Service locator: classes pull dependencies from a central registry. This hides what a class actually needs (the dependency is not visible in its signature), creating implicit coupling and complicating tests.
5 / 5
What is an IoC container?
IoC container: Inversion of Control containers manage object lifecycles and automatically build the dependency graph based on registrations, injecting the right instances so you do not wire everything by hand.