The Game Panel Design record
The Game Panel
ADR-0002 Accepted

Select instances with parameter attributes

Created
Decided
Versions
History

Context

The type of a dependency often does not say which instance it needs. Several implementations or instances can share one interface. A parameter typed against the interface says what it needs but not which one, so the choice has to be made somewhere else.

How much is known about the instances varies. Sometimes every concrete class, and what maps to it, is known when the panel boots. Sometimes the concrete is decided only at runtime, from configuration or other state, by the subsystem that owns the instances.

The panel is dynamic and built for one purpose, and third-party modules consume what its subsystems provide. A module cannot be expected to know the exact concrete it will receive, least of all when configuration can change it.

Decision

I will have a dependency say which instance it needs with an attribute on its own parameter, and what acts on that attribute will depend on what is known. Where every concrete and its mapping are known at boot, a Named attribute maps a string, or a qualifier attribute maps its class, to a binding registered for the type, and a factory on that binding produces the instance where construction has to be dynamic. Where the concrete depends on configuration or other runtime state, a resolvable attribute is paired with a resolver belonging to the subsystem that owns the instances, and the resolver decides the instance entirely, with nothing about it known to the bindings. The consuming class plays no part in the choice, and 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 class a given consuming class receives for a dependency. It would mean knowing the exact concrete for each consumer. I rejected this because a third-party module cannot be expected to know that, particularly when configuration can change it, and because in a system this dynamic and built for one purpose it would add a great deal of complication where the correct bindings and resolution can instead be trusted to exist.

Contextual attributes, as in Laravel's container. A contextual attribute is either paired with a resolver closure registered for it, or carries a resolve() method of its own, which receives the attribute instance as a redundant argument. The panel's author built the original implementation of the idea and co-authored the one Laravel merged, which shipped both forms together; their shape comes from being added to an existing system under a strict backwards compatibility policy. I rejected this because a resolvable attribute paired with a resolver class is a more structured implementation of the same idea, free of the constraint of fitting an existing system.

Consequences

Easier:

  • A dependency will ask for what it needs on its own parameter, and neither its consuming class nor whatever registers it will have to know the concrete:

    public function __construct(
        #[Named('minecraft')] private ServerTemplate $template,
        #[Ghost] private ServerRepository $servers,
    ) {}
    
  • Where the mapping is known at boot, it will live in the bindings, as part of registration.

  • Where it is not, the subsystem that owns the instances will keep full control over producing them. Its resolver will itself be resolved through the container, so it can receive dependencies of its own.

Harder:

  • Nothing will check, when bindings and resolvers are registered, that every name, qualifier and resolvable attribute in use has something to match it. A resolvable attribute with no registered resolver will fail when a dependency carrying it is resolved.
  • A subsystem that decides its instances at runtime will define both an attribute and a resolver, and register the pair.

Constrained:

  • Code will select an instance through these attributes, never by naming a concrete for a consuming class.
  • Selection decided at runtime will follow the resolver shape, with ghost objects as its first use in the container.

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, ResolverRegistry and ResolverCatalogue.
  • PR #23, feat(container): Dependency Injection, merged , with named and qualified bindings and Ghost and GhostResolver as 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 d6910e4 · exported