🎁 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

Twelve-Factor App
A methodology of twelve best practices for building modern, scalable, maintainable software-as-a-service applications.
Technical Debt
The implied cost of additional rework caused by choosing a quick solution now instead of a better approach that would take longer.
Design Pattern
A reusable solution template for commonly occurring problems in software design.
Closure
A function that captures and retains access to variables from its enclosing scope, even after that scope has finished executing.
Big O Notation
A mathematical notation that describes the worst-case performance of an algorithm as input size grows.
Singleton Pattern
A design pattern that restricts a class to a single instance and provides a global point of access to that instance.
View All Programming Concepts Terms →