I have a pretty nice idea for fine granular API command handling.
Our web API should provide single simple update endpoint, but you can provide multiple commands to it. Something like this:
POST /myAggregate/12345/update
[
{ command1Name: "command1Data" },
{ command2Name: "command2Data" },
{ command3Name: "command3Data" }
]
Within Axon this seems quite difficult to handle. Especially in combination with @AggregateVersion.
My first idea was to have a new UpdateWrapperCommand. which internally has a List commands. And then within the Aggregate, you use reflection to call the correct @CommandHandler methods:
class UpdateWrapperCommand {
List commands;
}
@Aggregate
class MyAggregate {
// id, version, constructor, etc. pp.
@CommandHandler
public void handle(SomeCommand cmd) { ... }
@CommandHandler
public void handle(UpdateWrapperCommand cmd) {
// iterate over cmd.commands
// iterate over this.getClass().getMethods()
// find correct method(s), and invoke it
}
}
But when the @CommandHandlers also use @MetaData and/or Spring Bean injection, then it's getting really hard.
My second idea was to simple call commandGateway.send in a loop. But that blows up, because @TargetAggragateVersion must be set for every command, and you have to wait for every command to complete, before sending the next one. That's not nice.
Do you have any ideas for that?
It should load the aggregate once and then run all commands.
And maybe even have some transaction-like behavior: Apply all resulting events, or none.