mjdi

Search documentation

Search the mjdi v0.10.0 documentation

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

#ExampleDemonstratesGolden output
01basic_wiringMinimal automatic wiring: Inject[T] fields, class providers, one resolve builds the chainHello, Mojo!
02value_providersApplication-supplied configuration via register_value + set_value, has_value, scope value mirroringdb://prod (retries=3) | premium=on
03factory_providersregister_factory: construction owned by a plain function, per-resolve freshness, factory failure propagation (in tests)ok:users:1 | ok:orders:2 | ok:cache:1
04alias_bindingsregister_alias: lifetime inheritance from the target provider, transient contrastr1 | r1 | plain:r2
05singleton_lifecycleprovider_scope.singleton: one canonical construction per container, copies handed to consumersv1:charge-100 | v1:charge-250
06scoped_sessionsprovider_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
07custom_constructionOverriding the Injectable.create contract with a DependencyResolver to derive stateattempts=5 delay=50 | attempts=3 delay=120
08layered_appA four-layer dependency chain (controller → service → repository → gateway), mixed lifetimes, explicit validate_registrycontroller>valid;db:insert order-42
09transient_pipelinesDefault transient lifetime: fresh instances per resolution, state isolation between consumersstage:alpha#1 | stage:beta#2 | stage:gamma#1 | stage:a#1+stage:b#2
10mixed_providers_appCapstone: 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
11linear_resourcesLinear 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 tracelinear resources: txn committed twice, lease cleaned on error, lock released
12testing_patternsTesting patterns (P1-08/P1-09): production registry, derived test registries via override_value/override, value inventories, expected-failure assertions, scope isolation, statistics-based lifetime checksPASS 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

  1. 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's renderer field.
  2. transient_pipelines — the default lifetime, made observable: stage counters reset per resolution, proving each consumer received a fresh instance.
  3. singleton_lifecycle — the contrast: one canonical construction per container (v1 persists across consumers), copies handed out.
  4. scoped_sessions — the per-unit-of-work middle ground, including the close_scope() release and the "same session returns, different session does not" demonstration.
  5. value_providers — configuration entering the graph from outside, has_value checks, and scope value mirroring.
  6. factory_providers — construction logic as a plain function; the counts in the output (users:1, orders:2, cache:1) show per-resolve freshness.
  7. alias_bindings — lifetime binding without subtyping: the alias inherits the target's scope, and the transient contrast shows what changes.
  8. custom_construction — the create override deriving a clamped retry budget (attempts=5 from a configured 8), the canonical Shape-1 override from Injectable.
  9. layered_app — a realistic four-level chain with mixed lifetimes and an explicit validate_registry call before the container exists.
  10. 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.py measures),
  • 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.