-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Command handlers should be optional by default #30
Comments
What about using pattern matching? def execute(state, %OpenAccount{} = command) do
[...]
end This forces commands to be defined. There might be a mismatch between the CamelCase command and the snake_case function name. |
@Papipo I've updated the issue to use |
Fixed by #35. |
What is the purpose of command handlers if they can just be implemented with pattern matching and |
@brendanzab By default you can route commands directly to the aggregate and use pattern matching on its I like to keep my aggregate functions pure. Therefore, if I need to pull in external data (e.g. third party system, read model projection) I do this in a command handler. Then pass this additional data to the aggregate. You don't need to use command handlers if you prefer not. |
Configuring command dispatch using the
Commanded.Commands.Router
macro forces you to implement a command handler per command.Often the command handler will just delegate to the aggregate root. It does nothing more and adds no value.
To reduce boilerplate code the command handlers should be optional. The default behaviour will be to dispatch the command to the aggregate.
A proposal for routing without a command handler. The
to:
key is the aggregate root itself and theaggregate:
key is not required.By default the command will be dispatched to an aggregate root function named
execute
. Pattern matching will ensure that it invokes the correct function for a given command struct.Consider making the command to function mapping configurable. As an example the function could be selected by the name of the command. So a command named
OpenAccount
maps to the functionopen_account/2
.It must still be possible to configure a command handler for the cases where they are required. An example would be if you need to request additional data for the command from elsewhere (e.g. external HTTP request).
The text was updated successfully, but these errors were encountered: