liveBook · Manning

Dependency injection

As explained in Microservices Patterns, Second Edition

Dependency injection is a technique in which an object's or component's dependencies are supplied from outside rather than constructed internally.

Dependency injection supplies a component’s dependencies from outside rather than having it construct them internally. It supports testability by allowing troublesome dependencies to be replaced with test doubles. section 5.4.3

Replacing heavyweight services with simulated dependencies can make tests simpler and faster: tests can run as in-memory unit tests instead of relying on integration-test infrastructure. section 5.4.4

Substitute a test double

If a component depends on an external service, inject a test double that simulates it. For database access, inject a mock DAO to avoid the database in a unit test. section 5.4.3 section 5.4.4

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains test-double substitution and its testing benefits without detailing implementation mechanics.
Examples
Demonstrates simulating an external service and mocking a DAO to avoid database access in tests.
What you can do after
Apply it to substitute a troublesome service or DAO with a test double and enable faster in-memory unit tests.
How it connects
Connects dependency injection with test doubles, unit testing, integration testing, and functional-style programming.

Also explained in

Compare how 28 books explain dependency injection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.