Decorators — Aquilia Documentation
Comprehensive guide and documentation for Decorators in the Aquilia framework. View API reference, examples, and implementation patterns.
Dependency Injection / Decorators DI Decorators & Metadata Aquilia provides clear annotations under aquilia/di/decorators.py to configure scopes, inject instances, and define factory dependencies declaratively. Inject Dataclass The Inject dataclass is used within typing.Annotated hints to instruct providers on how to resolve dependencies: from dataclasses import dataclass from typing import Any, Optional @dataclass class Inject: token: Optional[Any] = None # Override resolution token tag: Optional[str] = None # Disambiguate between multiple providers optional: bool = False # Resolves to None if unregistered Usage with Annotated from typing import Annotated from aquilia.di import Inject class OrderService: def __init__( self, # Resolved by parameter type hint repo: OrderRepository, # Tagged resolution (disambiguate multiple CacheBackends) cache: Annotated[CacheBackend, Inject(tag="redis")], # Optional resolution (defaults to None if missing) metrics: Annotated[MetricsClient, Inject(optional=True)], ): self.repo = repo self.cache = cache self.metrics = metrics Internal Extraction: The container uses typing.get_type_hints(cls.__init__) to extract these metadata markers during manifest processing, creating highly optimized static execution plans. inject() A shorthand helper that generates Inject configurations: from aquilia.di import inject class OrderService: def __init__( self, cache: Annotated[CacheBackend, inject(tag="redis")] ): self.cache = cache Dep (Per-Request Dependency Injection) FastAPI-Style Injection: Dep is Aquilia's modern approach to inline route injection. It allows you to declare dependencies directly in route signatures, bypassing manifest declarations for route-specific tools. Dependencies declared in route signatures form a per-request Directed Acyclic Graph (DAG) resolved concurrently: from typing import Annotated from aquilia.di import Dep from aquilia.controller import Controller, get async def get_db_session(): async with db.session() as session: yield session class UserController(Controller): prefix = "/users" # UserController constructor injection is still used for core services def __init__(self, auth: AuthService): self.auth = auth @get("/ ") async def get_user( self, ctx, db_session: Annotated[DbSession, Dep(get_db_session)] # resolved per-request ): user = await db_session.query(User).filter_by(id=ctx.request.params["user_id"]).first() return ctx.json(user) Registration Decorators Decorator Scope Description , , , , , , ].map((row, i) => ( ))} Scopes Lifecycle )
Go to Homepage