Knowledge Maxxing
← Back to topic library
Dev MaxxingIntermediateSoftware Design75 sec

Dependency injection

Dependency injection is supplying a piece of code with the other components it needs from outside, rather than having it create or look up those components itself.

Prerequisite: Separation of concerns

What matters

Because dependencies come from outside, they can be swapped, such as for a test double during testing, without changing the code that uses them.

Example

Passing a database connection into a function as a parameter, rather than the function creating its own connection internally, is dependency injection.

Common misconception

Dependency injection is not a specific tool or framework feature; it's a general principle that can be applied with or without any special library support.

Recall question

What does dependency injection make easier to swap during testing?

Keywords and related concepts

dependency injectiontestabilitysoftware designseparation of concerns

Related topics

Separation of concerns
Dev MaxxingIntermediate
Test double
Dev MaxxingIntermediate