mjdi docsv0.10.0latest
Examples Guide
The examples/ directory ships twelve runnable applications — one per
Source: mjdi/docs/examples-guide.md
The examples/ directory ships twelve runnable applications — one per
mjdi capability, plus the linear-resource and testing-pattern showcases —
alongside the original single-file walkthrough. Each example is a
runnable Mojo package (app.mojo + __init__.mojo) that prints a
deterministic report, and each has a dedicated test file
(tests/test_example_<name>.mojo) that exercises every struct and
function the example declares; the suite also executes each app and checks
its golden output, and a static coverage gate requires 95% of the example
declarations to be referenced by tests (currently 100% — see
Testing).
Run any example from the project root:
mojo run -I . examples/<name>/app.mojo
The catalog
| # | Example | Demonstrates | Golden output |
|---|---|---|---|
| 01 | basic_wiring | Minimal automatic wiring: Inject[T] fields, class providers, one resolve builds the chain | Hello, Mojo! |
| 02 | value_providers | Application-supplied configuration via register_value + set_value, has_value, scope value mirroring | db://prod (retries=3) | premium=on |
| 03 | factory_providers | register_factory: construction owned by a plain function, per-resolve freshness, factory failure propagation (in tests) | ok:users:1 | ok:orders:2 | ok:cache:1 |
| 04 | alias_bindings | register_alias: lifetime inheritance from the target provider, transient contrast | r1 | r1 | plain:r2 |
| 05 | singleton_lifecycle | provider_scope.singleton: one canonical construction per container, copies handed to consumers | v1:charge-100 | v1:charge-250 |
| 06 | scoped_sessions | provider_scope.scoped + create_scope(): per-unit-of-work instances, independent scopes, close_scope() | session[req-1] ready | session[req-2] ready | session[req-1] ready | session[req-3] ready |
| 07 | custom_construction | Overriding the Injectable.create contract with a DependencyResolver to derive state | attempts=5 delay=50 | attempts=3 delay=120 |
| 08 | layered_app | A four-layer dependency chain (controller → service → repository → gateway), mixed lifetimes, explicit validate_registry | controller>valid;db:insert order-42 |
| 09 | transient_pipelines | Default transient lifetime: fresh instances per resolution, state isolation between consumers | stage:alpha#1 | stage:beta#2 | stage:gamma#1 | stage:a#1+stage:b#2 |
| 10 | mixed_providers_app | Capstone: value + factory + class (singleton/transient/scoped) + custom create in one checkout application, plus compile-time registry introspection | --usd-- ord-1;reserved:2/100;saved:ord-1:keyboard | ... | scopes=transient,transient,singleton,transient,transient,scoped |
| 11 | linear_resources | Linear resources (P1-21): a transaction, a failing file lease (error-path cleanup), a lock token, and a shared pool that survives linear destruction; destruction-exactly-once and the linear trace | linear resources: txn committed twice, lease cleaned on error, lock released |
| 12 | testing_patterns | Testing patterns (P1-08/P1-09): production registry, derived test registries via override_value/override, value inventories, expected-failure assertions, scope isolation, statistics-based lifetime checks | PASS examples/testing_patterns |
Legacy single-file walkthrough: e2e_example.mojo
— the PRD's developer-experience tour, also executed by the test runner
(golden: [info] User-42).
Suggested reading order
- basic_wiring — the whole library in twenty lines: two structs, two
registrations, one resolve. Notice that the application never calls a
constructor with arguments; the container wires
GreetingService'srendererfield. - transient_pipelines — the default lifetime, made observable: stage counters reset per resolution, proving each consumer received a fresh instance.
- singleton_lifecycle — the contrast: one canonical construction per
container (
v1persists across consumers), copies handed out. - scoped_sessions — the per-unit-of-work middle ground, including the
close_scope()release and the "same session returns, different session does not" demonstration. - value_providers — configuration entering the graph from outside,
has_valuechecks, and scope value mirroring. - factory_providers — construction logic as a plain function; the
counts in the output (
users:1,orders:2,cache:1) show per-resolve freshness. - alias_bindings — lifetime binding without subtyping: the alias inherits the target's scope, and the transient contrast shows what changes.
- custom_construction — the
createoverride deriving a clamped retry budget (attempts=5from a configured8), the canonical Shape-1 override from Injectable. - layered_app — a realistic four-level chain with mixed lifetimes and
an explicit
validate_registrycall before the container exists. - mixed_providers_app — everything at once in a checkout flow, plus a
registry-introspection report (
kinds=...,scopes=...) computed from the registry type itself.
How the tests verify each example
Each tests/test_example_<name>.mojo:
- exercises every struct and function the example declares (that is
what
scripts/example_coverage.pymeasures), - asserts the example's golden
run_demo()output end to end, - covers the error paths belonging to the demonstrated feature — missing value, scoped-from-container rejection, factory failure, clamping branches.
Run one directly:
mojo run -I . tests/test_example_value_providers.mojo
or the whole suite:
bash scripts/run_tests.sh
Using the examples as templates
The examples are deliberately small and copy-pasteable. A new application
usually starts life as layered_app (chain shape) plus value_providers
(configuration) plus the lifetime pieces it needs. Keep each example's
discipline when you scale it up: one composition root, deterministic
run_demo() output, and a test that exercises every declaration — that is
what keeps the static coverage gate meaningful as your code grows.