Services — The Complete DI Pattern
The previous chapters established the building blocks: capabilities (plain types in the typed
environment R), caps! / HCons lists, and Layers (recipes that build capabilities). Now we
put them together into the complete service pattern.
A service in id_effect is the combination of:
- A trait defining the interface (methods return
Effects). - A capability type that holds an implementation in
R— usually a newtype overArc<dyn Trait>. - One or more implementations (production and test).
- A
Layerthat builds the capability, possibly from other capabilities.
Requirements are part of the effect's type (caps!(UserRepo, Email)); a program whose R is not
fully provided does not compile. By the end of this chapter you'll have a multi-service program
wired at the edge with layers and tested with plain values.
The sections Tokio bridge through Logging apply the same pattern to the workspace integration crates: running effects on Tokio, platform I/O and HTTP, hosting handlers under Axum / Tower, and injecting configuration, logging and OpenTelemetry. Read them in order when wiring a production binary.