|
| 1 | +# Custom DI |
| 2 | + |
| 3 | +**python-cq** abstracts dependency injection behind the `DIAdapter` protocol, allowing you to integrate any DI framework. |
| 4 | + |
| 5 | +## The `CQ` class |
| 6 | + |
| 7 | +`CQ` is the central object that binds together the handler registries and the DI adapter. The module-level decorators (`command_handler`, `event_handler`, `query_handler`) and bus factories (`new_command_bus`, `new_event_bus`, `new_query_bus`) all derive from a default `CQ` instance created at import time. |
| 8 | + |
| 9 | +You can create additional isolated instances when you need separate handler registries, for example in tests or in a multi-tenant setup: |
| 10 | + |
| 11 | +```python |
| 12 | +from cq import CQ, ContextCommandPipeline |
| 13 | + |
| 14 | +cq = CQ(my_di_adapter).register_defaults() |
| 15 | + |
| 16 | +command_handler = cq.command_handler |
| 17 | +event_handler = cq.event_handler |
| 18 | +query_handler = cq.query_handler |
| 19 | + |
| 20 | +new_command_bus = cq.new_command_bus |
| 21 | +new_event_bus = cq.new_event_bus |
| 22 | +new_query_bus = cq.new_query_bus |
| 23 | +``` |
| 24 | + |
| 25 | +When using `ContextCommandPipeline`, pass `cq.di` explicitly so it uses the same DI adapter: |
| 26 | + |
| 27 | +```python |
| 28 | +ContextCommandPipeline(cq.di) |
| 29 | +``` |
| 30 | + |
| 31 | +## Implementing a `DIAdapter` |
| 32 | + |
| 33 | +`DIAdapter` is a `Protocol`. Implement it to integrate any DI framework: |
| 34 | + |
| 35 | +```python |
| 36 | +from collections.abc import Awaitable, Callable |
| 37 | +from cq import CQ, DIAdapter, CommandBus, EventBus, QueryBus |
| 38 | +from typing import Any, AsyncContextManager |
| 39 | + |
| 40 | +class MyDIAdapter(DIAdapter): |
| 41 | + def command_scope(self) -> AsyncContextManager[None]: |
| 42 | + # Return an async context manager that: |
| 43 | + # - opens a DI scope for the duration of a command dispatch |
| 44 | + # - manages the lifecycle of a RelatedEvents instance within that scope |
| 45 | + # - silently ignores nested activations (re-entrant calls) |
| 46 | + ... |
| 47 | + |
| 48 | + def lazy[T](self, tp: type[T]) -> Callable[[], Awaitable[T]]: |
| 49 | + # Ask the DI framework for a resolver for `tp`. |
| 50 | + # The returned callable, when called and awaited, performs the resolution. |
| 51 | + ... |
| 52 | + |
| 53 | + def register_defaults( |
| 54 | + self, |
| 55 | + command_bus: Callable[..., CommandBus[Any]], |
| 56 | + event_bus: Callable[..., EventBus], |
| 57 | + query_bus: Callable[..., QueryBus[Any]], |
| 58 | + ) -> None: |
| 59 | + # Register the bus factories as default providers so that handlers |
| 60 | + # can declare CommandBus, EventBus, or QueryBus as dependencies. |
| 61 | + # This method is optional; the default implementation is a no-op. |
| 62 | + ... |
| 63 | + |
| 64 | + def wire[T](self, tp: type[T]) -> Callable[..., Awaitable[T]]: |
| 65 | + # Return an async factory that instantiates `tp` with injected dependencies. |
| 66 | + # Used internally to build handler instances. |
| 67 | + ... |
| 68 | + |
| 69 | + |
| 70 | +cq = CQ(MyDIAdapter()).register_defaults() |
| 71 | +``` |
0 commit comments