mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi playground

Learn mjdi from verified examples: dependency declarations, registry composition, provider and lifetime selection, the exported dependency graph, the exact runtime output, and what the compiler does when the wiring is wrong.

Follow the tutorial · Getting started · Interactive graph viewer

Output provenance

Every graph, output and compiler rejection on this page was produced by the real toolchain — mjdi 0.10.0 on Mojo 1.2.0 (2016d0cd) — at release time () and frozen into this page by scripts/export_playground_artifacts.py. This playground does not compile or execute code on the server; the examples are runnable locally from examples/basic_wiring/app.mojo.

The smallest complete wiring: two class providers, one Inject edge, compile-time validation — runnable locally with mojo run -I . examples/basic_wiring/app.mojo.

# ===----------------------------------------------------------------------=== #
# Example 01: basic automatic wiring.
# ===----------------------------------------------------------------------=== #
# The smallest useful mjdi application.
#
# `GreetingService` depends on `MessageRenderer` through an `Inject[T]`
# field. Both are registered as class providers at compile time; the
# container wires the chain automatically when `GreetingService` is
# resolved. Missing providers would be rejected before any application
# code runs (see tests/test_dependency_graph.mojo for the failing case).
#
# Run from the project root:
#
#     mojo run -I . examples/basic_wiring/app.mojo
#

from mjdi import Container, Injectable, Inject, Registry


struct MessageRenderer(Injectable):
    """Formats the greeting text (the innermost service)."""

    def render(self, name: String) -> String:
        return String("Hello, ") + name + String("!")


struct GreetingService(Injectable):
    """The application entry-point service; mjdi injects the renderer."""

    var renderer: Inject[MessageRenderer]

    def greet(self, name: String) -> String:
        return self.renderer.value().render(name)


def run_demo() raises -> String:
    """Builds the registry, resolves the service, returns the report."""
    comptime app_registry = (
        Registry()
        .register[MessageRenderer]()
        .register[GreetingService]()
    )
    var app = Container[type_of(app_registry)]()
    var service = app.resolve[GreetingService]()
    return service.greet("Mojo")


def main() raises:
    print(run_demo())