forked from pgorecki/python-ddd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_bus.py
More file actions
22 lines (19 loc) · 1014 Bytes
/
Copy pathcommand_bus.py
File metadata and controls
22 lines (19 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from application.commands import Command, CommandResult
class CommandBus(object):
"""
Command bus is a central place for executing commands.
It offers some benefits over executing commands directly from the controller:
- in-memory bus can be replaced with persistent one, so that multiple applications can share same bus
- it can be used by different clients: web controller, console application, etc.
- we can provide rate limiting and protection against DOS attacks
- we can reject duplicated commands
"""
def __init__(self, command_handler_factory):
self._command_handler_factory = command_handler_factory
def get_handler_for_command(self, command: Command):
command_class_name = type(command).__name__
return self._command_handler_factory(command_class_name)
def execute(self, command: Command) -> CommandResult:
# mediator pattern??
handler = self.get_handler_for_command(command)
return handler.handle(command)