Dispatch
The core of the command dispatcher.
Attention
You do not have to add this middleware yourself, the Builder
will add
it for you.
Usage
The dispatch middleware actually handles the dispatching of requests to their handlers. It should be the last middleware in the chain.
Registration
You do not have to add this middleware yourself, the Builder
will add
it for you.
Instead you setup the middleware using with_locator()
and
with_factory()
to provide its dependencies.
Context
We add a Dispatch
context instance for each handler executed. It will
contain the result of the function call and the name of the handler.
result = message[banshee.Dispatch].result
name = message[banshee.Dispatch].name
Reference
- class banshee.Dispatch(name, result)
Bases:
object
Dispatch context.
Marks a message as having had its request processed by a specific handler and contains the result of that execution.
- name: str
handler name
- class banshee.DispatchMiddleware(locator, factory)
Bases:
Middleware
Dispatch middleware.
Implements the command dispatcher design pattern to route requests to their configured handlers.
Uses the pre-configured
HandlerLocator
to retrieve a all associatedHandlerReference
instances, before passing the references one by one to aHandlerFactory
in order to get andHandler
instance to call.A
Dispatch
context will be added to the message for each successful handler.- Parameters:
locator (HandlerLocator) – locator to lookup associated handlers for a message
factory (HandlerFactory) – factory to instantiate a concrete handler from a reference
- async __call__(message, handle)
Handle message.
Dispatch the request to all associated handlers and store the results.
- Parameters:
message (Message[T]) – message to process
handle (HandleMessage) – next middleware invoker
- Returns:
processed message
- Raises:
banshee.errors.DispatchError – when one or more handlers fails
- Return type:
Message[T]