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:
| Template | Shape | The point |
|---|---|---|
standard_app | Standard application | value config, singleton + transient services, lifecycle-aware cache, validate_runtime() before work, with_scope request isolation, close() shutdown |
cli_app | CLI application | options as a value provider, per-command services, runtime command dispatch, guarded invocations |
service_app | Long-running service | startup health via post_construct, container-owned singletons vs scoped per-request state, error-path scope cleanup |
test_app | Test application | production 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_scopelexical guard, - lifecycle management (
InjectableLifecyclehooks for releasable state), - error handling — startup validation failures, error-path scope cleanup,
- shutdown handling — explicit
close()withpre_destruct, - build/run instructions (the per-template README),
- tests — the self-testing
app.mojoassertions plus the runner'sPASSgate.
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).
Related
- Getting started — the minimal walk-through.
- Patterns — composition-root guidance in depth.
- Testing — the testing guide.