Dependency injection

What it is

Dependency injection supplies a component with the collaborators it
needs. A service that records reservations might receive a store and
a clock when it is constructed. Its logic can use those
collaborators through the agreed behaviour, while another part of
the program chooses the database implementation and how requests get
the current time.

The dependency can be passed through a constructor, a function
argument or another explicit setup step. Many frameworks offer a
container that builds and wires components by configuration. The
technique works with ordinary function calls too. A composition
point near program startup can select implementations, set up
resources and hand the configured component to the routes or jobs
that will call it.

Lifetime is part of the arrangement. A process-wide manager may own
a reusable database pool. A request can open a transaction or carry
per-request state. The component must get the collaborator that fits
its usage, and the program must arrange cleanup when the relevant
lifetime ends. A test can use a small substitute for the clock and a
realistic store if it needs to exercise the transaction boundary.
Those choices belong where the test and the service are assembled.

Why it matters

A component that constructs its own dependencies often combines
several decisions. It has to know how to calculate a result, where
the data lives, how time is obtained and which resources to close.
It can also be difficult to exercise a failing store or a chosen
expiry time without affecting other code. Supplying collaborators
can make the inputs and the ownership of those choices clearer.

Use a boundary that corresponds to real variation or a needed test.
A reporting component can receive an interface for obtaining a data
snapshot. A test can make that source fail at a known moment and
check the response. A component that can rely on a stable ordinary
library function may be easier to read without a wrapper. Each
introduced contract has a cost in how much callers and maintainers
need to learn.

The arrangement helps during changes to infrastructure. A new
adapter can be configured at the composition point and tested with
the component's existing operations. Keeping the business rules
separate from the wiring does not remove the need to test the
adapter. Its timeouts, consistency and cleanup still influence
outcomes. Supply a contract that explains what the caller can rely
on, and exercise an implementation with the boundary it really uses.

What it looks like in practice

Imagine a service that gives customers a short reservation hold. It
receives a clock and a store as constructor arguments. In
production, a composition function builds a store backed by the
application's database pool and supplies the relevant clock. A
focused test can supply a fixed time to check which side of a
boundary an operation falls on. A separate integration exercise can
use the real store and verify the persisted state throughout a
change.

Define lifetime alongside the wiring. A shared service can hold
references to reusable collaborators that are safe across requests.
It should receive a particular transaction through the operation
that uses it when a consistent transactional boundary is required.
Exercise concurrent calls and shutdown paths to check who owns
cleanup. A lifetime mismatch can be hard to diagnose if the code
explains only where an object was constructed.

Engineer's maritime platform architecture case study describes
separating presentation, request handling and database-owned
business logic. It gives a context for deciding what a request
handler should own. In this illustrative design, the handler
receives the boundary it calls and the wiring site chooses its
implementation. The choice of where a rule is enforced is a separate
decision that should follow the application and its data.

In a test, make the failure meaningful. Inject a store that refuses
an operation because its conditions were violated and confirm that
the caller handles the documented result. Pass a real adapter
through the same contract to test the deployment path. Agreeing on
the shape of a function call is useful, and representative failure
cases are how the dependency's behavioural contract becomes visible.

Where it goes wrong

An object can appear flexible while it reaches into a central
registry to look up arbitrary services. Its constructor then says
little about what it needs, and a wiring error can surface only when
a rarely used path first runs. Prefer to make the needed
collaborators visible at the boundary people instantiate. If a
framework resolves them, give important startup checks a way to
exercise the complete composition.

Configuration is another collaborator with lifecycle. Give the
service its request deadline as part of its configuration when
assembling the component. Validate that value when assembling the
component and test the result with a boundary configuration. This
makes a malformed setting visible during startup and gives a test a
direct way to set the state it needs. Use configuration types that
make important units and defaults clear.

A common lifecycle error stores a request-scoped collaborator in an
object shared by many requests. A later call can use the wrong
request state or a closed resource. Review which component owns each
resource and when the next call can run. Test overlapping requests
when lifetime influences correctness. Keep state passed explicitly
through the operation when it belongs only to that operation.

Finally, avoid abstractions that only repeat a stable API. Introduce
a boundary when the component has a reason to choose an
implementation, control lifetime or exercise an important contract.
Keep its responsibilities small enough that the wiring remains
readable. A good composition point gives maintainers a clear account
of where configuration becomes usable behaviour and where they can
verify the result.

Further reading

  * Microsoft .NET dependency injection. Explains injection, scopes
and service lifetimes.

  * Spring dependencies and collaborators. Describes ways components
declare their collaborators.

  * Jakarta Dependency Injection. Specifies injection annotations
and scope concepts for Java.

hThis page on the web	URL:https://engineer.company/glossary/dependency-injection/	engineer.company	70
