Convert return type of command handling constructor

From the docs:

In case the ‘command handling constructor’ has executed successfully, instead of the Aggregate itself, the value of the @AggregateIdentifier annotated field will be returned.

I’m using a complex value type for my aggregate. So instead of e.g. a UUID I’m using a MyAggregateId type.
In the core of my application this is fine, however, I prefer not to expose this concept outside of my domain in order to minimize coupling. Therefore, both the Commands and Events use a stringly typed ID.

Is it possible to change the default behavior so that the framework converts the complex value type to a stringly typed value for the constructor command handler?

Hello and welcome to the Axon community @viwjvyhybsxsolbokn!

Let me start by saying that registering with a temporary email is fine. The only drawback is on your side - you’ll be missing notifications (which can be disabled btw) when people answer your questions. If you don’t mind me suggesting something, please at least change your name to something human-readable, so you don’t appear as a spam bot or something at first glance :wink:

As for your question, the answer is, yes, you can do that. You can not change what a constructor returns, but you can use a method instead. Such a method will contain the logic you’d typically put in the constructor but can return your custom type. You then annotate it with @CreationPolicy(AggregateCreationPolicy.ALWAYS), and when it receives a Command, it will create a new instance of the Aggregate. Of course, ensure the Aggregate has an empty non-private constructor so the framework can actually instantiate it.

For more information about the CreationPolicy annotation, please see the documentation.

1 Like