ZCore LogoZCore
Core concepts

Dependency Injection (IoC Container)

Explore ZCore's custom Inversion of Control container, constructor auto-wiring, signature caching, and lifecycle management.

ZCore does not rely on FastAPI's native Depends() chains for internal service wiring. Instead, it implements a high-performance IoCContainer designed for constructor auto-wiring and clean architecture separation.


1. The Problem with Nested Depends()

In standard FastAPI, if a route depends on a service, the service depends on a repository, and the repository depends on an AsyncSession, you end up with deeply nested, tightly coupled Depends() chains in your HTTP layer:

# Standard FastAPI (High coupling & boilerplate)
@router.post("/tasks")
def create_task(
    service: TaskService = Depends(get_task_service) # get_task_service must resolve repo & session
):
    pass

This couples web routes directly to internal persistence details, degrades IDE type-hinting, and makes unit testing difficult.


2. The Inject[T] Abstraction

ZCore uses Python's Annotated metadata system to seamlessly bridge FastAPI's route dependency injection with ZCore's IoC container:

# ZCore Approach (Clean & Decoupled)
from zcore import Inject

@router.post("/tasks")
async def create_task(service: Inject[TaskService]):
    await service.create(...)

Compilation Magic: When you annotate a parameter with service: Inject[TaskService], the Inject class intercepts the __class_getitem__ dunder method. It compiles this into: Annotated[TaskService, Depends(Injector(TaskService))].

This preserves 100% compatibility with FastAPI's OpenAPI generation while delegating full dependency graph assembly to ZCore's IoC container.


3. Lifecycle Management & Scope Guarding

The IoCContainer coordinates three distinct instance lifecycles:

  1. Singleton: Instantiated once for the entire application lifespan and stored in container._singletons (e.g., EventDispatcher, Settings).
  2. Scoped: Instantiated once per execution context and bound to the coroutine context via ContextVar (_scoped_instances). Managed and automatically purged by ScopedDependencyMiddleware after the response is sent (e.g., AsyncSession).
  3. Transient: A fresh, independent instance is created every time container.resolve() is executed (e.g., stateless helper utilities).

Strict Scope Boundary Enforcement: Attempting to resolve a scoped dependency or AsyncSession outside of an active scope context (when _current_scope_id is None) immediately raises an explicit DIException:

DIException: Cannot resolve scoped dependency 'TaskService' outside of an active scope.

This Fail-Fast mechanism guarantees that database sessions and request-bound instances cannot accidentally leak into singletons, thread pools, or unmanaged background execution paths.


4. Auto-Wiring, Keyword Injection & Signature Caching

Inside your classes and services, you do not need decorators. Declare dependencies in the constructor via standard type hints:

class TaskService:
    # ZCore inspects the type hint and resolves TaskRepo automatically
    def __init__(self, repository: TaskRepo, timeout: int = 30, client: Any = None):
        self.repository = repository
        self.timeout = timeout
        self.client = client

When container.resolve(TaskService) is invoked:

  1. Inspection & Type Unwrapping (_unwrap_type): ZCore inspects the constructor parameters via inspect.signature and typing.get_type_hints. It recursively unwraps Annotated, typing.Union, and Python 3.10+ union types (such as IService | None = None) to locate the underlying dependency.
  2. Default Parameter Handling: If a constructor parameter declares a default value (param.default is not empty) and its type is not explicitly registered in the container (such as primitive int, str, or unregistered optional objects), the container skips auto-wiring for that parameter. Python then safely applies the declared default (e.g. timeout = 30). If a dependency is explicitly registered in the container, it takes precedence and is injected.
  3. Keyword-Based Auto-Wiring (**kwargs): Auto-wiring resolves dependencies into a named dictionary (resolved_kwargs) and constructs classes using target_class(**resolved_kwargs). This eliminates argument misplacement when optional/default parameters precede registered ones or when keyword-only parameters are utilized.
  4. Signature Caching (_DependencySignature): Evaluated constructor parameter maps are cached in _dependency_signature_cache using a specialized _DependencySignature structure. Subsequent resolutions bypass reflection completely, reading directly from the cache to construct instances with minimal reflection overhead.

5. Circular Dependency Detection

If ServiceA depends on ServiceB, and ServiceB depends on ServiceA, a naive container would enter infinite recursion and crash the interpreter.

ZCore tracks the active resolution graph using an internal _stack set during auto-wiring. If a class is encountered that already exists in the active stack, it raises a readable CircularDependencyError:

CircularDependencyError: Circular dependency detected: ServiceA -> ServiceB -> ServiceA

Constructor vs Route Usage:

  • In Class Constructors (__init__): Use plain Python type hints (def __init__(self, repo: TaskRepo):).
  • In FastAPI Route Endpoints: Use Inject[T] (async def route(service: Inject[TaskService]):).

6. Background Tasks & Isolated Scope (background_scope, @background_task)

When an HTTP request completes, the ASGI middleware disposes of the request-scoped database session and purges _scoped_instances. Running background routines after the request finishes requires an independent scope.

ZCore provides two utilities for safe background processing:

A. The @background_task Decorator

Decorate any asynchronous coroutine or standard synchronous function. The decorator:

  1. Spawns an isolated background_scope().
  2. Automatically allocates a fresh, dedicated AsyncSession.
  3. Inspects the function's signature and auto-resolves any missing type-annotated dependencies via container.resolve() using _unwrap_type.
  4. Executes synchronous functions in a dedicated worker threadpool via anyio.to_thread.run_sync.
import uuid
from zcore import background_task
from tasks.services import TaskService

@background_task
async def send_welcome_email(user_id: uuid.UUID, service: TaskService):
    # 'service' and its underlying AsyncSession are automatically resolved in an isolated scope
    await service.notify(user_id)

Avoid Passing Active Request Sessions: Do not pass an existing AsyncSession from an HTTP route directly into a background task. The request session will be closed when the HTTP response completes. Let @background_task inject an isolated session automatically; passing an active session triggers a runtime logger warning.

B. The background_scope Context Manager

For manual execution blocks, background_scope provides explicit boundary control:

from zcore import background_scope

async with background_scope(inherit_context=True, custom_flag="worker"):
    # Clean, isolated IoC and DB session lifecycle
    ...

On this page