Skip to content

Getting Started

Installation

Install dijay using your preferred package manager:

uv add dijay
pip install dijay

Python Version

dijay requires Python 3.13+ and leverages modern features like type parameter syntax and Annotated generics.

Your First App

1. Define a Service

Use the @injectable decorator to mark a class as injectable:

from dijay import injectable

@injectable()
class GreetingService:
    def greet(self, name: str) -> str:
        return f"Hello, {name}!"

2. Create a Module

Modules group related providers together. Use providers to register and exports to share:

from dijay import module

@module(
    providers=[GreetingService],
    exports=[GreetingService],
)
class GreetingModule: ...

3. Create the Root Module

The root module imports all feature modules:

@module(imports=[GreetingModule])
class AppModule: ...

4. Bootstrap and Resolve

Use Container.from_module to build the dependency graph and resolve services:

import asyncio
from dijay import Container

async def main():
    async with Container.from_module(AppModule) as container:
        service = await container.resolve(GreetingService)
        print(service.greet("World"))

if __name__ == "__main__":
    asyncio.run(main())

Running this will print:

Hello, World!

How It Works

  1. @injectable() marks GreetingService as a provider.
  2. @module(...) groups providers and exports them.
  3. Container.from_module(AppModule) scans the module tree and registers all providers.
  4. async with container runs @module.on_bootstrap hooks on entry and @module.on_shutdown hooks on exit.
  5. container.resolve(GreetingService) returns a fully constructed instance with all dependencies injected.

Next Steps