I am working on my first service using ddd.
My domain layer looks simillar to:
/ domain
/A
/aggregateA.py -> fires an EventA
/aggregateARepository.py
/B
/aggregateB.py
/aggregateAService.py <-- Implements an event handler that listens to EventA
So there are multiple event/command handlers in each subfolder.
In the api layer i would like to execute the commands/events.
@router.post("/",..)
async def add_a():
cmd = CommandA()
//execute CommandHandler and EventHandlers for the events that have been raised.
Is there a way to register event/command handlers inside my domain layer other than having:
/domain
handlers.py
/a
...
/b
...
where handlers.py looks like this (lists all handlers)
command_handlers = { CommandA: a.command.command_handler_func,
...}
event_handlers = { EventA, [ handler1, handler2 , ...]
I tried the following:
handlers.py:
command_handlers = { } # empty, registration happends in each sub module
and then:
domain/a/commandA.py:
from domain.handlers import command_handlers
def command_handler_a(CommandA):
....
command_handlers[CommandA] = command_handler_a
and the same for each commands/events in the other subfolders.
This does not seem to work as expected. The command_handlers are empty unless i import domain/a/commandA.py.
To fix this I would have to import all handlers from the subfolders in a signle place, right ? Which defeats the point I'm trying to archieve, decentralized registration.
Is there a best practice for doing this kind of registration ? I am used to frameworks in other languages where all I have to do is annotate the command handler @command_handler, and it magically works.