mjdi docsv0.10.0latest
Third-Party Types Guide
How to wire structs you do not own — types from other libraries, generated
Source: mjdi/docs/third-party.md
How to wire structs you do not own — types from other libraries, generated
code, or stdlib types — into an mjdi graph. mjdi 0.4.0 introduces the
use_* registration family for exactly this; this guide is the
complete picture, including the boundaries.
The problem
mjdi's default wiring path constructs a service through its Injectable
contract: default-construct, then populate Inject[T] fields. That
requires the struct itself to conform to Injectable — and Mojo declares
trait conformance at the type-definition site. A struct from another
library cannot be retrofitted after the fact, so without extra support a
third-party type could only enter your graph through
register_factory[...]() with transient semantics, never with a cached
lifetime, and never through the value path's consistent spelling.
The use family
Registry (and every chained registry) exposes two additional methods
that are the third-party spellings of the existing registration kinds:
from mjdi import Registry
from mjdi.provider import provider_scope
comptime app_registry = (
Registry()
.use_value[ExtConfig]() # value provider
.use_provider[ExtConn, connect_ext]() # transient
.use_provider[ExtPool, open_ext_pool, provider_scope.singleton]() # singleton
.use_provider[ExtSession, open_session, provider_scope.scoped]() # scoped
)
| Method | Semantics | Requirements on the third-party type |
|---|---|---|
use_value[T]() | Value provider: registration records the kind; the value is supplied per container with set_value[T](/docs/0.10.0/v); consumers receive copies. | Copyable |
use_provider[T, make_t]() | Function-provided, transient: make_t runs once per resolution; the caller owns the product. | Movable & Deinitable |
use_provider[T, make_t, provider_scope.singleton]() | Function-provided, singleton: make_t runs once per container; the canonical product is cached and consumers receive copies. | Copyable |
use_provider[T, make_t, provider_scope.scoped]() | Function-provided, scoped: one canonical product per Scope; consumers receive copies; must be resolved through a scope. | Copyable |
Notes that hold for the whole family:
- Same machinery, same guarantees.
use_*registrations produce the same provider descriptors as theirregister_*equivalents, so duplicate detection, graph validation, introspection (kind_of/scope_of), diagnostics, and slot semantics are identical. Verified end to end bytests/test_use_providers.mojo. - Duplicate rules are the same: registering the same type twice — via
any mix of
register_*anduse_*— fails compilation. - The provider function follows the factory rules: compile-time
registered, runtime invoked, no mutable captures, may
raise(the error propagates to the resolver). It is never invoked at registration or at container construction — only at the first resolution that needs the product (container.has_value[T]()lets you observe this:Falseafter construction,Trueafter the first resolve of a cached product). - Cached products require
Copyable— consumers receive copies of the canonical product (Mojo value semantics). The check fires at registration:mjdi: singleton factory provider 'ExtPool' requires the provided type to conform to Copyable ...— fixable by making the type copyable or dropping to the transient scope. - Scoped third-party products obey the scoped rules exactly: resolving
from the container raises ("can only be resolved through a Scope"),
close_scope()releases the cached product, and the next resolve re-constructs.
What you can do with third-party types in the graph
Inject them into your own services. Your Injectable structs declare
Inject[TheirType] fields like any other dependency; compile-time
validation checks the registration exactly as for your own types:
struct ExtService(Injectable):
var config: Inject[ExtConfig]
var pool: Inject[ExtPool]
def report(self) -> String:
return self.config.value().url + String(" pool=") + String(self.pool.value().stamp)
Introspect them. use_* registrations answer the same compile-time
queries — type_of(registry).kind_of[T]() and scope_of[T]() — as
register_* ones (example: tests/test_use_providers.mojo).
Test them. Third-party structs are defined in the test file exactly like the external types they stand in for; see Testing.
Boundaries (by design)
-
No aliasing from third-party types.
register_alias[From, To]constructsFromthrough itsInjectablecontract, so the alias source must conform toInjectable. If the goal is "resolveTheirTypewith a particular lifetime", register it directly withuse_providerand that scope — the observable behavior is the same, without an alias. -
No dependency declaration inside third-party types. They cannot declare
Injectfields, so mjdi never wires into them. If an external object needs collaborators, your provider function supplies them:def open_ext_pool() raises -> ExtPool: var pool = ExtPool() pool.connect("ext://primary") # the function owns the wiring return pool^ -
No retrofitted
Injectable. mjdi does not generate adapters or wrappers. If you want a third-party type to participate in auto-wiring, wrap it: a thinInjectablestruct holdingInject[TheirType]gives it a place in the graph while mjdi keeps handling the third-party instance through its registered provider.
Decision summary
| Need | Use |
|---|---|
| External value (config object, parsed settings) | use_value[T]() + set_value[T] |
| External object, fresh per resolution (connection, transaction) | use_provider[T, make_t]() |
| External object, expensive construction (pool, cache client) | use_provider[T, make_t, provider_scope.singleton]() |
| External per-unit-of-work object | use_provider[T, make_t, provider_scope.scoped]() |
| External type with a friendlier graph identity | wrap it in your own Injectable struct |
| "Their type, but with that other provider's lifetime" | use_provider with that scope (aliases cannot start at third-party types) |