Hi Richard,
you’re right about the “separate Create or Update commands”. They are preferred over the find-or-create approach, but it’s not always possible to work around them.
To implement find-or-create in Axon, you need an “external command handler”, which is a normal bean with an @CommandHandler on it. You command handler code would look like:
try {
aggregate = repository.load(aggregateid)
aggregate.doUpdate();
} catch (AggregateNotFoundException e ) {
aggregate = new MyAggregate(…);
repository.add(aggregate);
}
Having to catch an exception may not seem like the most beautiful approach, but then again, find-or-create isn’t very beautiful if you look at it from a DDD/CQRS perspective.
Also consider, as you mentioned, an approach where you do a query in the query database to find out if you need to do an update of a create. This model may be eventually consistent with the command model, but the chance of false positives/negatives is still extremely small.
Cheers,
Allard