How to handle commands for aggregates with IDs assigned after command?

118 views
Skip to first unread message

Jonathan Meraz

unread,
Mar 26, 2019, 2:15:42 PM3/26/19
to Axon Framework Users
I know the subject line doesn't make sense given the way Axon works, but here is my problem:

I need to create a new instance of aggregate, "Quote", that is tied to a backend system of record. That is, the aggregate ID must eventually match the ID assigned in the backend system. So, my uiServer app is calling commandGateway and sending it a CreateQuoteCmd, but I don't know what to pass as the target aggregate ID since the ID will come from a backend system called by the command handler. The uiServer cannot assign the quoteId. The command handler for CreateQuoteCmd contacts our backend system to get the new quoteId, but then how do I make that quoteId the ID for the aggregate?

How to do this?

Thanks for your help.

Jonathan Meraz

unread,
Mar 26, 2019, 4:21:54 PM3/26/19
to Axon Framework Users
Perhaps the ultimate question is:

Is it true that I must pass a target aggregate ID in CreateQuoteCmd instead of allowing the object to set its own ID in the command handler after communication with the backend system?

Thanks for your help.

Jakob Hatzl

unread,
Mar 27, 2019, 3:14:01 AM3/27/19
to Axon Framework Users
Hi Jonathan,

we're doing something similar in our application here. For our UI we allow client-side generated GUIDs and generate a random one on the server side if the client is not capable of generating a GUID.
All of this happens in the controller/business logic before the command is dispatched to the command gateway. The CreateXXXCommand then takes a GUID which is set to the @AggregateIdentifier field in the Aggregate via the Events applied in the Constructor-Command Handler.

For your case it should be possible to retrieve the desired ID from your backend system in the business logic before dispatching the command and (as you stated in your second post) pass it in the CreateQouteCommand to set it in your aggregate.

Hope this helps,
Best Regards,
Jakob

Jonathan Meraz

unread,
Mar 27, 2019, 9:06:53 AM3/27/19
to Axon Framework Users
Below is the answer I received on stackoverflow from Steven van Beelen at AxonIQ. Basically, I'll just get the quoteId from the backend system while executing the command handler that is a constructor for the aggregate.

The Command which will create an Aggregate is not inclined to have a @TargetAggregateIdentifierannotated field. This holds as the field which is the 'target aggregate identifier', cannot point to an existing aggregate, as that command will be the starting point of an aggregate.
 
The creation of the Aggregate Identifier can happen at several points in your system, and is really up to you.
 
The important part here though is that the @CommandHandler annotated constructor within an Aggregate has a return value, which is the Aggregate Identifier you have assigned to that Aggregate.
You should thus handle the result given to you from the CommandGateway/CommandBus when dispatching your CreateQuoteCmd. This should contain the QuoteId you have assigned to your (I assume) Quote Aggregate.

Steven van Beelen

unread,
Apr 2, 2019, 5:09:41 AM4/2/19
to Axon Framework Users
Hi Jonathan, Jakob,

Ah, nice, happy to see I have already answered your question on Stack Overflow.
And, thank you for sharing the solution with everybody here as well!

Cheers,
Steven

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages