Retun increment id in Command creation

Hello,

I am working on a monolith strangulation strategy.
The monolith uses many mysql tables with Integer ids.

For the migration of the first domain, a little group of tables has identified like an aggregate for the v2 platform.
Problem: the integer id of the aggregate is referenced many times from other tables, I need to keep this id and deal with it in the v2 platform.

In my v2 POC, I have a command model with a mysql counter in order to reserve an integer Id before apply the event creation (while the command handling).

The command creation return the UUID, I need to return also the integer, for referencing in the v1 platform.

What is the cleaner way to do this ?

Thank you,
Have a nice year.

Hi Frederic,

the standard way of implementing creation command handlers is by annotating a constructor with @CommandHandler. The downside of a constructor is that the “return value” of a constructor is always the just constructed instance. In an Axon based application, you will not want to treat the entire aggregate as a return value for a Command. That’s why we return the identifier instead.

Normal methods don’t have this limitation; you can return whatever you want.
Since Axon 4.3, you can use different creation policies on Command Handlers. By annotating a Command Handler method with @CreationPolicy, you can define whether you want Axon to invoke the method on an existing one (default), or to create a new instance (the only option for constructors). You can even tell it to update an existing one if it exists, or otherwise create a new one (but that is potentially an expensive operation).

So in your case, I would use a normal instance method, which returns (among other data, perhaps) the generated identifier. Annotate that method with @CreationPolicy(AggregateCreationPolicy.ALWAYS) and you’re good to go.

See https://docs.axoniq.io/reference-guide/axon-framework/axon-framework-commands/command-handlers#aggregate-command-handler-creation-policy for examples.

ok, that’s very clear.

It works perfectly, thank you very much Allard.