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.
- Docs: https://mjdi.mojo.asyncmove.com
- Source: GitHub - jymchng/mjdi: Compile-time-first dependency injection for Mojo: type-declared dependencies, compile-time validated graphs, runtime resolution. · GitHub (MIT)
- Version
v0.10.0, built and tested against Mojo 1.2.0
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 makeInjectable. - 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
overridefamily swaps a real provider for a test double. - Lifecycle: an optional
InjectableLifecycletrait withpost_construct/pre_destructhooks 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.mojoccomes from the file
name you pass tomojo precompile -o, somjdi-0.10.0.mojoccan only be imported asmjdi-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
comptimeregistry andContainer[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!