Buses are the message-passing layer of CQRS. They decouple the sender from the handler: callers dispatch a command or ask a query without knowing which class handles it or where it runs. HexCore provides three abstract ports —Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Indroic/HexCore/llms.txt
Use this file to discover all available pages before exploring further.
AbstractCommandBus, AbstractQueryBus, and AbstractEventBus — plus in-memory implementations (InMemoryCommandBus, InMemoryQueryBus, InMemoryEventBus) that resolve handlers from a HandlerRegistry, run them through a MiddlewarePipeline, and support Smart Routing for background execution.
CQRS exceptions
Both abstract and concrete buses raise typed exceptions fromhexcore.domain.cqrs.exceptions:
HandlerNotFoundError
The command, query, or event class that had no registered handler.
DuplicateHandlerError
HandlerRegistry when attempting to register a second handler for a type that already has one and allow_override=False.
The command or query class for which a duplicate registration was attempted.
Abstract contracts
AbstractCommandBus
dispatch
The command instance to dispatch.
The value returned by the command handler.
None for fire-and-forget commands.HandlerNotFoundError — if no handler is registered for the command’s type.
AbstractQueryBus
TResult parameter.
ask
The query instance to resolve.
The data returned by the query handler, typed to match the query’s generic parameter.
HandlerNotFoundError — if no handler is registered for the query’s type.
AbstractEventBus
publish
The domain event to publish to all registered subscribers.
subscribe
The event class to subscribe to.
An async callable invoked whenever an event of
event_type is published.Backward-compatibility aliases
hexcore.domain.cqrs.buses and are fully equivalent.
In-memory implementations
InMemoryCommandBus
@background_command are automatically enqueued to a task backend instead of being handled locally, provided enqueuer and serializer are configured.
Constructor
The handler registry used to resolve command handlers.
Optional middleware pipeline. An empty pipeline is used when not provided.
Task queue adapter for Smart Routing. Required if any registered command uses
@background_command.Serializer for converting command instances to task payloads. Required alongside
enqueuer.dispatch
- Checks
type(command).__cqrs_background__. IfTrueandenqueuer/serializerare configured, the command payload is serialised and sent to the task queue (using the queue name from__cqrs_queue__, defaulting to"default"). ReturnsNoneimmediately. - If the command is not marked for background execution, resolves the handler from the registry and executes it through the middleware pipeline synchronously.
InMemoryQueryBus
Constructor
The handler registry used to resolve query handlers.
Optional middleware pipeline applied before the handler is called.
ask
type(query) from the registry and executes it through the middleware pipeline, returning the handler’s result.
InMemoryEventBus
@background_handler. Each handler is evaluated independently: some may run locally while others are routed to a task queue.
Constructor
Middleware pipeline applied to every published event before handlers are invoked.
Task queue adapter. Required for handlers decorated with
@background_handler.Serializer for converting event instances to task payloads. Required alongside
enqueuer.subscribe
handler to the subscriber list for event_type. Multiple handlers may be registered for the same event type.
The event class to subscribe to.
The async event handler callable.
publish
type(event). For each subscriber:
- If the handler has
__cqrs_background_handler__ = Trueandenqueuer/serializerare set, the event payload is serialised and enqueued usingenqueuer.enqueue_handler(handler_name, payload, queue=queue_name). - Otherwise, the handler is called directly through the middleware pipeline.