Skip to main content

CQRS dispatcher integrating python-cqrs-core with gridflow-python-mediator

Project description

Python CQRS Dispatcher

CQRS dispatcher that integrates python-cqrs-core with gridflow-python-mediator for type-safe command/query dispatch.

Installation

pip install python-cqrs-dispatcher

Features

  • Type-Safe Dispatch: Separate command/query dispatch methods
  • Handler Registry: Track registered handlers
  • Pipeline Behaviors: Built-in support via mediator
  • Bulk Registration: Register multiple handlers at once
  • Auto-Discovery: Auto-register handlers from modules

Usage

Basic Setup

from python_cqrs_dispatcher import CQRSDispatcher
from python_cqrs_core import BaseCommand, ICommandHandler

class CreateUserCommand(BaseCommand):
    name: str
    email: str

class CreateUserHandler(ICommandHandler[CreateUserCommand, int]):
    async def handle(self, command: CreateUserCommand) -> int:
        # Create user logic
        return user_id

# Setup dispatcher
dispatcher = CQRSDispatcher()
dispatcher.register_command_handler(
    CreateUserCommand,
    CreateUserHandler()
)

# Dispatch command
cmd = CreateUserCommand(name="John", email="john@example.com")
user_id = await dispatcher.send_command(cmd)

With Queries

from python_cqrs_core import BaseQuery, IQueryHandler

class GetUserQuery(BaseQuery):
    user_id: int

class GetUserHandler(IQueryHandler[GetUserQuery, User]):
    async def handle(self, query: GetUserQuery) -> User:
        return await db.get_user(query.user_id)

dispatcher.register_query_handler(GetUserQuery, GetUserHandler())

query = GetUserQuery(user_id=1)
user = await dispatcher.send_query(query)

Bulk Registration

from python_cqrs_dispatcher import register_handlers

handlers = [
    (CreateUserCommand, CreateUserHandler()),
    (UpdateUserCommand, UpdateUserHandler()),
    (GetUserQuery, GetUserHandler()),
    (ListUsersQuery, ListUsersHandler()),
]

register_handlers(dispatcher, handlers)

Pipeline Behaviors

from gridflow_python_mediator import LoggingBehavior, TimingBehavior

# Add behaviors (execute before all handlers)
dispatcher.add_pipeline_behavior(LoggingBehavior().handle)
dispatcher.add_pipeline_behavior(TimingBehavior().handle)

Auto-Registration

from python_cqrs_dispatcher import auto_register_handlers
import my_app.handlers

# Auto-discover and register all handlers
auto_register_handlers(dispatcher, my_app.handlers)

API Reference

CQRSDispatcher

Type-safe CQRS dispatcher.

Methods:

register_command_handler(command_type, handler)

Register command handler.

Parameters:

  • command_type (Type[ICommand]): Command class
  • handler (ICommandHandler): Handler instance

register_query_handler(query_type, handler)

Register query handler.

Parameters:

  • query_type (Type[IQuery]): Query class
  • handler (IQueryHandler): Handler instance

async send_command(command) -> Any

Dispatch command.

Parameters:

  • command (ICommand): Command to dispatch

Returns:

  • Command result

async send_query(query) -> Any

Dispatch query.

Parameters:

  • query (IQuery): Query to dispatch

Returns:

  • Query result

add_pipeline_behavior(behavior)

Add pipeline behavior.

Parameters:

  • behavior (Callable): Behavior function

Helper Functions

register_handlers(dispatcher, handlers)

Bulk register handlers.

Parameters:

  • dispatcher (CQRSDispatcher): Dispatcher instance
  • handlers (List[Tuple[Type, Any]]): Handler tuples

auto_register_handlers(dispatcher, module)

Auto-discover and register handlers.

Parameters:

  • dispatcher (CQRSDispatcher): Dispatcher instance
  • module (Module): Python module to scan

Dependencies

  • python-cqrs-core>=0.1.0
  • gridflow-python-mediator>=0.1.0

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_cqrs_dispatcher-0.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_cqrs_dispatcher-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file python_cqrs_dispatcher-0.1.1.tar.gz.

File metadata

  • Download URL: python_cqrs_dispatcher-0.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_cqrs_dispatcher-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6aeb42bbeb035fa508b76da655be9f480ee1ba6501d2aa26582292039bfda446
MD5 845e0efc2568c48e491b5ff58f303c0f
BLAKE2b-256 7515b459340624445300a618f401fb50d8455a92bbf8817959b6430900ce290a

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_cqrs_dispatcher-0.1.1.tar.gz:

Publisher: publish-pypi.yml on firstunicorn/python-web-toolkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_cqrs_dispatcher-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_cqrs_dispatcher-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5cdd9691fcb2590303354e3bf18adbdbbfa8de39ec08c605b4c1d1d3fc1146fc
MD5 fff0c6a0176515dea82a1ae0e2b827da
BLAKE2b-256 af8f3b33608f7a5fe5eeeffaa8a8f7fe94f47af58e47be2d62c7f6d9cd2fd417

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_cqrs_dispatcher-0.1.1-py3-none-any.whl:

Publisher: publish-pypi.yml on firstunicorn/python-web-toolkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page