Introducing mjdi: compile-time-first dependency injection for Mojo

Hi everyone,

I’ve been working on mjdi, a dependency injection library for Mojo. The idea is to move as much as
possible of the wiring problem to compile time: services declare what they need through types, the
provider set lives in the type of a registry, and the compiler validates the whole dependency graph
before your program runs.

What it looks like

from mjdi import Container, Injectable, Inject, Registry

struct UserRepository(Injectable):
    def find_by_id(self, user_id: Int) -> String:
        return String("User-") + String(user_id)

struct Logger(Injectable):
    def info(self, message: String) -> String:
        return String("[info] ") + message

struct UserService(Injectable):
    var repository: Inject[UserRepository]
    var logger: Inject[Logger]

    def get_user(self, user_id: Int) -> String:
        return self.logger.value().info(self.repository.value().find_by_id(user_id))

def main() raises:
    # The provider set is encoded in the registry's type, at compile time.
    comptime app_registry = (
        Registry()
        .register[UserRepository]()
        .register[Logger]()
        .register[UserService]()
    )
    var app = Container[type_of(app_registry)]()
    var service = app.resolve[UserService]()
    print(service.get_user(42))  # [info] User-42

Dependencies are plain Inject[T] fields, found through compile-time reflection. There are no strings
and no decorators to keep in sync with the code.

Mistakes become compile errors

Forget to register a provider and the build stops with:

mjdi: no provider is registered for ‘UnregisteredDep’. Register it with Registry().register[…] (or
a value/factory/alias provider) before resolving it. [diagnostic MJDI-E011]

Cycles, duplicate registrations, lifetime mismatches and invalid aliases are rejected the same way.
Every diagnostic has a stable ID and an entry in a catalog (diagnostics/catalog.json), so you can
search for it and tools can recognise it. Each guaranteed rejection has a compile-fail test in the
suite: 33 of the 114 checks are programs that must fail to compile, asserting the message.

What is in v0.10.0

  • Providers: types, values, factories and aliases; use_* registration for third-party types you
    cannot make Injectable.
  • Lifetimes: singleton, transient and scoped, plus named and child scopes.
  • Keyed (qualified) registrations, multi-bindings, optional dependencies, provider decorators,
    assisted injection (runtime arguments next to injected ones), registry bundles and conditional
    provider selection.
  • Testing: the override family swaps a real provider for a test double.
  • Lifecycle: an optional InjectableLifecycle trait with post_construct / pre_destruct hooks and
    Container.close().
  • Introspection: graph export (JSON / Graphviz DOT), graph diff, explain-resolution, lifecycle
    tracing and cache statistics.
  • A benchmark suite and 10+ runnable example applications.

What it is not

It is a DI library, not an application framework. No HTTP, configuration loading or logging, and the
scope will stay that way. It is also young: one compiler version is supported (Mojo 1.2.0), the API is
pre-1.0 and may change, and I have not used it in a large production codebase.

Try it

Grab the precompiled package from the GitHub release together with its checksums:

curl -LO https://github.com/jymchng/mjdi/releases/download/v0.10.0/mjdi.mojoc
curl -LO https://github.com/jymchng/mjdi/releases/download/v0.10.0/SHA256SUMS
sha256sum -c --ignore-missing SHA256SUMS      # macOS: shasum -a 256 -c --ignore-missing SHA256SUMS
mkdir -p lib && mv mjdi.mojoc lib/
mojo run -I lib your_app.mojo

Two gotchas I hit while packaging it, in case they save you time:

  • The file must be named exactly mjdi.mojoc. The module name inside a .mojoc comes from the file
    name you pass to mojo precompile -o, so mjdi-0.10.0.mojoc can only be imported as mjdi-0.10.0.
  • Pass the directory (-I lib), not the file.

Or clone the repo and run mojo run -I . examples/basic_wiring/app.mojo.

The docs site has every page as raw markdown (a “Copy page” button, and /api/docs/<version>/<page>.md),
version-tagged URLs, and an MCP endpoint (/api/mcp) if you want your coding agent to read them.

Feedback I’d love

  • Does the compile-time registry style feel natural in your code, or does it get in the way? The
    comptime registry and Container[type_of(registry)] are the part I am least sure about.
  • Real-world wiring shapes it should support: web handlers with per-request scopes, plugin systems,
    CLI tools?
  • Diagnostics: where was a message unclear or missing?
  • Is anyone building a framework (web, CLI, agents) that would want to sit on top of it? I am happy to
    work on an integration.

Issues and PRs are welcome. Thanks for reading!

Compile time dependency injection is appealing for exactly the reason you describe, and the cost lands in rebuild times and in how errors read: a missing binding becomes a compile error somewhere upstream of the code that needed it, which is better than a runtime null but harder to read than a stack trace. The other thing worth deciding early is whether the container is allowed to be constructed conditionally, because the moment a binding depends on a runtime flag the compile time approach has to be worked around rather than used.