What is Dependency Injection?
A design pattern where objects receive their dependencies from external sources rather than creating them internally.
Dependency injection (DI) passes dependencies to a class instead of having the class create them. Instead of class UserService { db = new Database() }, you write class UserService { constructor(db) }. This makes code testable, flexible, and loosely coupled.
Types include constructor injection, setter injection, and interface injection. DI containers (Spring, Angular's injector) automate dependency resolution. DI is fundamental to testable code — mocking dependencies becomes trivial.