Skip to main content

Dependency Injection

Introduction​

A service usually needs other services to work, and something has to decide how they are built and handed over. The blocks in this group describe that wiring with types, so that a missing dependency, or one of the wrong type, is a compile error rather than a failure at startup.

The Blocks​

The group has two blocks, which work at different points in an application's life.

BlockArtifactWhat It Is For
Wirezio-blocks-scopeCompile-time safe recipes for constructing a service and its dependencies
Contextzio-blocks-contextType-indexed heterogeneous collections

How the Blocks Differ​

Wire is a recipe: it describes how to construct a service and what that service needs, and the compiler validates the whole dependency graph. It ships in the same artifact as the resource-management types, because a wire builds services that may hold resources, and the Resource Management page shows how wires, resources and scopes fit together.

Context is a container: an immutable collection that stores values of different types, indexed by their types. A phantom type parameter tracks which types are present, so looking up a value that is not there does not compile.

Where to Start​

Start with Wire to assemble an application from its parts, and read Context when a program needs to carry a set of services around by type.