Skip to main content

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

Project description

Python CQRS Dispatcher

CQRS dispatcher that integrates python-cqrs-core with 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 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
  • 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.0.tar.gz (5.7 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.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_cqrs_dispatcher-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for python_cqrs_dispatcher-0.1.0.tar.gz
Algorithm Hash digest
SHA256 80b1002bcb3ca4b67cf8a5ae3506886dc1b59f40cce02dec87f7f80e334225da
MD5 7d9e79a55f31a04de951782891313cd9
BLAKE2b-256 cdba6b6d3696c56ccdf3cee572ae7601b220420ee16bc5cd7dcf48f788076211

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_cqrs_dispatcher-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 978748551a9a47486475d09ac8a1d3a5b8687704d4e7845abb0657a19010d80e
MD5 2c62b4492865735593379d42a2c7bafc
BLAKE2b-256 b637d36fa41d7244e96fe0a3e44f169bb3d8710f147a83ac9063fabc30eb9043

See more details on using hashes here.

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