liveBook · Manning

Indirection

As explained in Unit Testing Principles, Practices, and Patterns

An intermediate abstraction layer that separates a caller from the concrete implementation or solution it ultimately needs.

Indirection inserts an intermediate abstraction layer between a caller and the concrete implementation or solution it needs. It can separate domain logic, application entry points, and infrastructure, but each added layer increases navigation and reasoning costs. section 8.5

Use indirection selectively:

  • Keep a clear boundary between domain classes and controllers so unit and integration tests have distinct targets. section 8.5
  • Prefer as few layers as possible; backend systems can often use a domain layer, application services layer, and infrastructure layer. section 8.8
  • Replace callback-shaped circular dependencies with a value object returned from the callee. section 8.8
  • Treat interfaces with one in-process implementation as a warning sign when they exist mainly to support mocks. section 8.8

Return the result instead of calling back

A checkout service that passes itself into a report generator creates a circular dependency. Refactor the generator to return a Report, allowing checkout to continue its work directly and removing unnecessary indirection.

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Contrasts useful boundaries with excessive layers and demonstrates callback removal, stopping short of implementation-level mechanics.
Examples
Uses checkout and report services, callback refactoring, and a three-layer backend architecture.
What it assumes
Assumes familiarity with domain models, controllers, unit tests, integration tests, interfaces, and callbacks.
What you can do after
Evaluate and simplify abstractions by preserving useful boundaries, reducing layer count, and replacing circular callbacks with returned values.
How it connects
Connects domain models, application services, infrastructure, controllers, integration testing, mocking, and circular dependencies.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.