psincraian / commandbus

Command Bus Pattern in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

commandbus

PyPI version Download stats version

πŸ“œ About

Command bus is a pattern from CQRS. In this pattern we have three components:

  • Command: represents the desired action to be done, like RegisterUserCommand.
  • CommandHandler: retrieves the data from the command and executes all the actions to accomplish the command objective. In the previous case creates a new user and saves it do database.
  • CommandBus: routes the command to the handler

βš’οΈ Installation

Installation is so easy, you only need to execute

pip install commandbus

πŸš€ Using commandbus

Using command is as easy as type

from commandbus import Command, CommandHandler, CommandBus

class SomeCommand(Command):
    pass

class SomeCommandHandler(CommandHandler):
    def __init__(self):
        self.called = False

    def handle(self, cmd: Command):
        self.called = True
        
bus = CommandBus()
handler = SomeCommandHandler()
bus.subscribe(SomeCommand, handler)
assert not handler.called
bus.publish(SomeCommand())
assert handler.called

🚩 License

The code is available under the MIT license.

About

Command Bus Pattern in Python

License:MIT License


Languages

Language:Python 100.0%