🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Programming Concepts Intermediate

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.

Related Terms

Code Smell
A surface indication in code that usually corresponds to a deeper problem in the system, suggesting the need for refactoring.
Algorithm
A step-by-step procedure for solving a problem or performing a computation, defined as a finite sequence of instructions.
Refactoring
Restructuring existing code without changing its external behavior to improve readability, maintainability, and performance.
Hash Table
A data structure that maps keys to values using a hash function, providing average O(1) time complexity for lookups, insertions, and deletions.
Race Condition
A bug that occurs when the behavior of software depends on the timing or order of uncontrolled events like thread scheduling.
Closure
A function that captures and retains access to variables from its enclosing scope, even after that scope has finished executing.
View All Programming Concepts Terms →