mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

Registry overrides: typed test composition (P1-09)

The override family gives mjdi a first-class, typed override

Source: mjdi/docs/registry-overrides.md

The override family gives mjdi a first-class, typed override mechanism: derive a test registry from a production registry by replacing selected providers, without duplicating any production registration code.

comptime production = (
    Registry()
    .register_value[Env]()
    .register[Gateway, provider_scope.singleton]()
    .register[Client]()
)

# The test registry: the same graph with the gateway replaced.
comptime test = (
    Registry()
    .register_value[Env]()
    .register[Gateway, provider_scope.singleton]()
    .override_value[Gateway]()      # pre-built test double
    .register[Client]()
)

The family

MethodReplaces withThe typical test use
.override[T, scope]()a class provider (lifetime optional, default transient)lifetime surgery: make a singleton transient per test
.override_value[T]()a value providerinject a pre-built test double with set_value[T]
.override_factory[T, factory, scope]()a factory providerreplace construction wholesale

Semantics

  • Explicit override semantics. An override replaces the active provider for an already-registered service. Overriding a type that was never registered is a compile-time error (MJDI-E008; compile-fail fixture cf_override_missing_target) — override replaces, it does not add.
  • Compile-time invalid override detection. The registration guards apply to the replacement exactly as they do to a plain registration: a non-Copyable value override (MJDI-E002), a cached factory override of a non-Copyable product, and — at container construction — an override whose target is not registered. Registering the same type again after an override is still a duplicate (MJDI-E004; fixture cf_override_then_register_duplicate): only the override family may replace providers.
  • Type safety. Every override is typed: the replacement provider serves the same service type, so consumers' Inject[T] fields keep compiling unchanged.
  • Clear precedence: the override wins. Registry lookups dispatch head-first, and the override node is the head, so provides, kind_of, scope_of, supply_as, and graph validation all see the override. Overriding twice is legal — the later override wins.
  • The replaced node stays in the chain — its graph validation still holds (its own dependencies must remain registered) — but it is never consulted again: lookups find the override first. This is the same last-wins head-first dispatch that alias re-registration has always used; alias override semantics are unchanged.
  • Predictable Registry composition. Override methods are pure registry composition, like every register* method: they return a new registry value.

After an override

  • container.set_value[T](...) follows the override: if T was overridden from class to value, set_value now applies; if it was overridden from value to class, set_value no longer applies (compile-time, MJDI-E013).
  • graph_json / explain_resolution report the override as the active provider ("registration": "override[T]"), with the overridden lifetime — the export mirrors resolution dispatch, so tooling sees what the container will actually do.

Testing with overrides

The canonical test-registry derivation (see Testing and examples/testing_patterns):

# A pre-built double for Gateway:
def fake_gateway(region: String) -> Gateway:
    var double = Gateway()
    double.env = Inject[Env](Env(region))
    return double^

var test_app = Container[type_of(test)]()
test_app.set_value[Env](Env("ignored"))
test_app.set_value[Gateway](fake_gateway("test"))
# Every consumer of Gateway now receives the double.