mjdi docsv0.10.0latest
FAQ
Short answers to the questions that come up most often. Each answer links
Source: mjdi/docs/faq.md
Short answers to the questions that come up most often. Each answer links to the page with the full story.
Setup and basics
Which Mojo version does mjdi require?
Mojo 1.2.0 — tested against mojo 1.2.0 (2016d0cd). Several design
decisions are pinned to 1.2.0 behavior (no @property, def only, __enum
syntax); see Known limitations and the
Technical investigation matrix.
How do I add mjdi to a project?
Copy the mjdi/ package directory in and run with -I .. Optionally
mojo precompile mjdi -o mjdi.mojoc (mojo package is deprecated in
1.2.0). See Getting started.
Why Container[type_of(app_registry)] and not Container[app_registry]?
The registry is a compile-time value; a type parameter cannot bind a value
directly, and type_of is the compiler-supported spelling for turning the
value into its type. Deviation 1 in
Known limitations.
Dependencies and services
Why self.dep.value() and not self.dep.value?
Mojo 1.2.0 has no @property support (verified: "use of unknown
declaration 'property'"), so the accessor is a method. One pair of
parentheses; see Dependency declaration.
Why can't I call a mutating method through an Inject reference?
value() returns an immutable view of the stored dependency; mutating
methods need an owned receiver. Resolve into an owned var and mutate
that. This is Mojo's ownership model, not a mjdi restriction —
the immutable-reference rule.
Do all my services need a default constructor? Only class-provided types (they are default-constructed, then wired — two-phase construction). Types with required constructor arguments should be value- or factory-provided instead; Injectable.
My struct has no fields and won't compile.
Fieldless structs get no synthesized zero-argument __init__ on Mojo
1.2.0. Add def __init__(out self): pass (finding 28 in the
investigation matrix).
Can two services depend on the same type? Yes — diamonds are fine. Each gets its own resolution (transient) or its own copy of the canonical instance (singleton/scoped), per the provider's scope. Cycles are the thing that cannot happen: Compile-time validation → Cycles.
How do circular dependencies fail? At type-definition time — the Mojo compiler rejects recursive struct layouts ("attempt to resolve a recursive reference"). mjdi additionally runs a cycle walk at validation as defense in depth. Compile-time validation.
Providers and lifetimes
Why does register_value not take the value?
A compile-time registry cannot store runtime values — the PRD anticipated
exactly this split. Registration records the kind; the value is supplied
per container with set_value[T] (deviation 4,
Known limitations).
Why are factories always transient? Factory-level caching is not implemented in 0.3 (deviation 5); wrap the factory product in a singleton class provider if you need caching. Providers → Factory.
Why do singletons hand out copies instead of the instance?
Mojo value semantics: there is no safe second reference to one owned
instance. mjdi caches the canonical construction and copies it per
resolution, which is also why cached lifetimes require Copyable —
Ownership and memory.
How do I actually share state between services, then? Behind an explicitly shared handle registered as a value provider — every consumer copies the handle, which still refers to the same underlying resource. Ownership and memory → Sharing observable state.
Why does my singleton still show the old configuration after set_value?
It was constructed once, before the change, and snapshot the value at
construction. Resolve a transient for the new value, or build a new
container/scope — demonstrated in the
tutorial.
Can I resolve a scoped service directly from the container?
No — that raises with a diagnostic telling you to use
container.create_scope(). Scoped providers cache per Scope value;
Scopes and lifetimes.
Are singletons resolved inside a scope shared with the container? No — on 1.2.0 they are cached in the scope (a scope owns its own chain instance and cannot safely reference the parent's state). Documented deviation; transient/scoped behavior unaffected Known limitations §2.
Can I use an alias for "interface → implementation"?
Not really — there is no subtyping in Mojo's value system, and an alias
constructs the requested type with the target's lifetime configuration.
For implementation swapping, prefer composition (an Inject[Impl] field
inside your abstraction); use aliases for lifetime binding —
Providers → Aliases vs composition.
Can a type be registered twice? Class/value/factory: no — duplicate registration is a compile-time error. Aliases may re-alias an already-registered type (last wins), which is the documented override mechanism (Providers).
How do I register a struct from a third-party library?
With the use_* family: use_value[T]() for a value the application
supplies, use_provider[T, make_t]() with an explicit provider function —
transient by default, or cached with
use_provider[T, make_t, provider_scope.singleton]. Third-party types
cannot conform to Injectable (Mojo fixes trait conformance at the
type-definition site), and they cannot be alias sources; the complete
rules and boundaries are in Third-party types.
How do I run initialization/cleanup logic for my services?
Conform to InjectableLifecycle alongside Injectable: post_construct
runs after mjdi has wired every Inject field (per transient resolution;
once per canonical singleton/scoped construction), and pre_destruct runs
right before mjdi releases a cached canonical instance — when you call
Container.close() or Scope.close_scope(). Both hooks are required by
the trait; dropping a container without close() releases instances
without the hook. See Lifecycle hooks.
Validation and errors
When exactly does validation run?
At Container/Scope construction — compile-time elaboration, before any
application code. Registration-time guards exist as defense in depth but
are not authoritative, because of verified compiler quirks
Compile-time validation.
What fails at runtime instead of compile time?
Only what depends on runtime values: a value provider whose value was
never set, a factory that raises, an unresolved value() read, and a
scoped provider resolved outside a scope. The full boundary:
Compile-time validation → What is runtime by design.
Where is the list of all error messages? Diagnostics — exact message shapes, triggers, fixes, and the fixture that pins each one.
Testing and coverage
Does mojo test work?
It does not exist in Mojo 1.2.0. mjdi's suite is standalone programs driven
by scripts/run_tests.sh (54 checks); the same pattern works for your
project — Testing.
What does "100% coverage" mean here? Static API-surface coverage: every public declaration referenced from the executable artifacts (265/265 library, 88/88 example declarations), gated at 95%. It is not branch coverage — Mojo 1.2.0 has no instrumentation; the method and its honest limits are in Testing → Coverage methodology.
How do I test that my project's misconfigurations keep failing? Copy the compile-fail harness: compile the bad configuration expecting failure and grep for the diagnostic fragment — twenty lines of shell, shown in Testing → Compile-fail tests.
Scope of the library
Is mjdi a framework? No — no HTTP, routing, controllers, modules, middleware, ORM, configuration loading, CLI, event bus, or plugins. Value providers carry values; mjdi does not load them. The boundary: Core concepts → What mjdi is not.
Can I pass the container into my services? You can (it is a value), but don't — that is the service-locator anti-pattern and it hides the graph from consumer-aware validation. Resolve at the composition root; inject values Patterns → Anti-patterns.
Is there a disposal/teardown hook?
Yes, as an opt-in trait: conform to InjectableLifecycle and mjdi calls
post_construct after wiring and pre_destruct before it releases a
cached instance (at Container.close() / Scope.close_scope()). Both
hooks are required by the trait; implicit teardown (dropping the container)
releases instances without the hook. See
Lifecycle hooks and
Ownership and memory.
What does mjdi cost at runtime? Provider dispatch and validation are compile-time specialized per registry; runtime work is construction, factory calls, slot copies, and field writes. mjdi makes no measured performance claims — nothing in the test suite benchmarks it — but nothing stands between your resolve and a constructor call except a statically-dispatched supply function. Architecture.