Dependencies select their instance through Select instances with parameter attributes
- Comparing
- v1 to v2
- Current
- Read the document
Context
A dependency's The type of a dependency often does not determine say which instance it needs. Several Several
implementations or instances can
can share one interface: every database connection is a Connection. A parameter typed against that the interface says
says what it needs, but needs but
not which one, so the choice has to be expressed made somewhere else.
What How much is known about the instances varies. Sometimes every concrete class, and what maps maps
to it, is known when the panel
panel boots. Sometimes the concrete is only decided only at runtime, from configuration or other other
state, by the subsystem that
that owns the instances.
The panel is dynamic and built for one purpose, and third-party modules modules consume what its its
subsystems provide. A
A module cannot be expected to know the exact final concrete it needswill receive, especially when least of all when
configuration can change it.
Decision
A dependency says I will have a dependency say which instance it needs with an attribute on its own own
parameter, and what acts on that attribute
depends attribute will depend on what is known. Where every concrete concrete
and its mapping are known at boot, a Named attribute maps a string,
, or a qualifier qualifier
attribute maps its class, to a binding binding registered for the type, and a factory on that binding
produces the instance where construction needs has to be dynamic. Where the concrete depends on configuration or other
runtime other runtime
state, a resolvable attribute attribute is paired with a resolver a
resolver belonging to the subsystem that owns the instances,
, and the resolver decides the the
instance entirely, with nothing about it known to the bindings. The consuming class
class plays no part in the choice, and the the
correct bindings and resolvers are trusted to exist.
Alternatives
Contextual binding by consuming class, as in Laravel's container, where registration states which concrete a
which
concrete class a given consuming class receives for a a
dependency. It requires would mean knowing the exact final concrete for each consumer,
which . I rejected this
because a third-party module module cannot be expected to knowknow that, particularly when configuration configuration
can change it. In , and because in a system
system this dynamic and built for one purpose, purpose it would add a great deal of complication, complication
where the correct bindings and
bindings and resolution can instead be trusted to exist.
Contextual attributes, as in Laravel's container. Their shape comes from being added to an existing system under
a strict backwards compatibility policy. A contextual attribute is either paired with a resolver closure registered
closure
registered for it, or carries a resolve() methodmethod of its own, which receives the attribute instance as a redundant redundant
argument. The panel's
s author built the original implementation of the idea and co-authored the one Laravel merged, which which
shipped both
both forms together; their shape comes from being added to an existing system under a strict backwards
compatibility policy. A I rejected this because a resolvable attribute attribute paired with with
a resolver resolver class is a more structured implementation of the same
same idea, without free of the constraint constraint
of fitting an existing system.
No other alternatives were weighed.
Consequences
Easier:
-
A
dependency asksdependency will ask for what it needs on its own parameter, and neither itsconsumingconsuming class nor whateverregistersregisters ithaswill have to know the concrete:public function __construct( #[Named('secondaryminecraft')] privateContractServerTemplate $contracttemplate, #[Database('replica')Ghost] privateConnectionServerRepository $connectionservers, ) {} -
Where the mapping is known at boot, it
liveswill live in thebindingsbindings, as part of registration. -
Where it is not, the subsystem that owns the instances
keepswill keep full control over producing them.Its resolver isIts resolver will itself be resolved through the container, so itreceivescan receive dependenciesofof its own, as the database resolver receives the connection factory.
Harder:
- Nothing
checkswill check, when bindings and resolvers are registered, that every name,qualifierqualifier and resolvableattribute inattribute in use has something to match it. Aresolvableresolvable attribute with no registered resolverfailswill fail when a dependencycarryingcarrying it is resolved. - A subsystem that decides its instances at runtime
defineswill define both an attribute and a resolver, andregisters theregister the pair.
Constrained:
- Code
selectswill select an instance through these attributes, never by naming a concrete for a consuming class. - Selection decided at runtime
followswill follow the resolver shape. Ghost objects are its, with ghost objects as its first use in the container, and later subsystems follow it: the database connection attribute, providers for cycle-scoped instances, injected module manifests and registrars, and log channels.
Sources
- Issue #21, Engine - Dependency Injection, revision of in its edit history: named and qualified
bindings, factories on bindings, and custom resolvers, the first record of the decision, which is
created. - Commit 798470f, "Add resolution and invocation to the container", :
Resolvable,Resolver,ResolverRegistryandResolverCatalogue. - PR #23, feat(container): Dependency Injection, merged , with named and qualified bindings and
GhostandGhostResolveras the first resolvable pair:decided. When the decision was taken is not recorded. - Named and qualified bindings being for concretes known at boot, and resolvers for concretes decided by configuration or runtime state; resolvers being intended from the start, so that subsystems keep full control over their instances; the rejection of contextual binding by consuming class and why; and resolvers as a more structured implementation of the contextual attributes whose original implementation the panel's author built and whose merged implementation they co-authored, including how that implementation was shaped by backwards compatibility and the two forms it shipped with: first written down on .
- Laravel documentation, Service Container, "Contextual Binding" and "Contextual Attributes", read : the two Laravel mechanisms described as alternatives.
Source adr/0002-parameter-attributes.md · commit 9444a99 · published