Skip to content
Commitwise
Menu

Dependency injection

Also called: DI

Giving an object the things it needs from outside, instead of letting it create them itself.

Definition

With dependency injection (DI), a class receives its collaborators, such as an API client or a database, through its constructor or properties. That makes it easy to swap a real implementation for a fake in tests, and to change implementations without touching the class. Frameworks like Hilt and Spring automate the wiring, but plain constructor parameters are DI too.

In practice

OrderService(repository: OrderRepository) can be tested with an in-memory repository instead of a real database.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.