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

**URL:** <https://forum.modular.com/t/introducing-mjdi-compile-time-first-dependency-injection-for-mojo/3724>\
**Category:** Mojo\
**Created:** [November 29, 2026, 5:31pm UTC](https://forum.modular.com/t/introducing-mjdi-compile-time-first-dependency-injection-for-mojo/3724 "2026-11-29T17:31:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jymchng](https://avatars.discourse-cdn.com/v4/letter/j/6de8d8/32.png) [@jymchng](https://forum.modular.com/u/jymchng)\
**Post date:** [November 29, 2026, 5:31pm UTC](https://forum.modular.com/t/introducing-mjdi-compile-time-first-dependency-injection-for-mojo/3724/1 "2026-11-29T17:31:56Z")

</div>

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](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](https://github.com/jymchng/mjdi) (MIT)
- Version `v0.10.0`, built and tested against **Mojo 1.2.0**

## What it looks like

```mojo
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:

```sh
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!

---

<div class="post-metadata">

**Author:** ![ankitshetty394](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@ankitshetty394](https://forum.modular.com/u/ankitshetty394)\
**Post date:** [November 30, 2026, 8:10pm UTC](https://forum.modular.com/t/introducing-mjdi-compile-time-first-dependency-injection-for-mojo/3724/2 "2026-11-30T20:10:58Z")

</div>

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.
