Hi,
I think this question has been risen multiple times but it is still unclear for me how to handle following case.
I have 3 aggregate types: A, B and C
In order to create an instance of aggregate A by a command CreateAggregateA(aggregateBId, aggregateCId)
I need to validate that instances of Aggregates B and C with ids aggregateBId and aggregateCId do exists.
In case if some aggregate doesn’t exist I need to throw a domain error.
What would be a best way to do that in Axon?
I have though about following solutions:
-
Use External Command Handler.
Inject Aggregate Repository there, load aggregate by ID and in case of AggregateDoesntExist prevent aggregate creation by throwing a Domain Error. -
Use Saga and ask Aggregate B and C for Aggregate A creation approval.
-
send command to create Aggregate A.
-
start saga on Aggregate A creation
-
send 2 commands to Aggregates B and C and ask them with a command like: RequestAggregateACreation() (it is going be two different classes for Aggregates B and C)
-
wait for command responses and catch AggregateDoesntExist, then throw Domain Error
-
listen to events from Aggregate B and C with response like: AggregateACreationApproved() (also two different classes for Aggregate B and C)
-
when both approval received, send ConfirmAggregateACreation() command and as result an AggregateACreationConfirmed() event will be emitted.
-
Use some Registry Projection for aggregates B and C, inject it as Resource into AggregateACreationSaga and query something like DoesAggregatesBAndCExist?
And then wait for query response (which will block saga). -
Same as 3-rd point, but instead querying, use External CommandHandler and emit events like in point 2. Then saga can listen to them asynchronously.
-
Use Long Lived Saga (forever) to act as Registry from 3rd point. – that’s not make too much sense.
What would be better approach?