mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

Composition roots: starter templates (P1-05)

templates/ ships four complete composition-root starter templates.

Source: mjdi/docs/composition-roots.md

templates/ ships four complete composition-root starter templates. Each is a runnable, self-testing Mojo application that demonstrates the full composition-root checklist, and each has its own README with the structure, wiring decisions, and build/run instructions:

TemplateShapeThe point
standard_appStandard applicationvalue config, singleton + transient services, lifecycle-aware cache, validate_runtime() before work, with_scope request isolation, close() shutdown
cli_appCLI applicationoptions as a value provider, per-command services, runtime command dispatch, guarded invocations
service_appLong-running servicestartup health via post_construct, container-owned singletons vs scoped per-request state, error-path scope cleanup
test_appTest applicationproduction registry + derived test registries via overrides, inventories, expected-failure assertions, statistics-based lifetime checks

Run them from the project root:

mojo run -I . templates/standard_app/app.mojo
mojo run -I . templates/cli_app/app.mojo
mojo run -I . templates/service_app/app.mojo
mojo run -I . templates/test_app/app.mojo

scripts/run_tests.sh executes all four as part of the suite (each prints PASS templates/<name>), so the templates cannot rot.

What every template includes

  • a registry definition (the compile-time Registry().register* chain),
  • injectable services with Inject[T] dependencies,
  • dependency registration across provider kinds,
  • container initialization plus runtime value configuration,
  • value registration through register_value + set_value,
  • scope usage through the with_scope lexical guard,
  • lifecycle management (InjectableLifecycle hooks for releasable state),
  • error handling — startup validation failures, error-path scope cleanup,
  • shutdown handling — explicit close() with pre_destruct,
  • build/run instructions (the per-template README),
  • tests — the self-testing app.mojo assertions plus the runner's PASS gate.

The templates are framework-neutral: only mjdi and the standard library — no HTTP, CLI, logging, or configuration dependencies.

Choosing a shape

  • standard_app — services wired once at startup, work dispatched per request through scopes.
  • cli_app — configuration arrives as parsed values; commands are transient services chosen by runtime dispatch.
  • service_app — long-lived shared state is resolved from the container; per-request state is scoped; failing requests must not leak their scopes.
  • test_app — the test-registry derivation pattern (see Registry overrides and Testing).