CreationPolicy and CommandHandler "outside" the Aggregate

Hi!

I saw your new “CreationPolicy” Feature in Axon 4.3. Due to your example

`

public class GiftCard {

public GiftCard() {
// Required no-op constructor
}

@CommandHandler
@CreationPolicy(AggregateCreationPolicy.ALWAYS)
public void handle(IssueCardCommand cmd) {
// An IssueCardCommand-handler which will create a GiftCard aggregate
}

@CommandHandler
@CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING)
public void handle(CreateOrRechargeCardCommand cmd) {
// A ‘CreateOrRechargeCardCommand’-handler which creates a GiftCard aggregate if it did not exist
// Otherwise, it will update an existing GiftCard aggregate.
}
// omitted aggregate state, command handling logic and event sourcing handlers
}

`

we could use the CREATE_IF_MISSING policy. In contrast to your example, we have our CommandHandlers outside of the Aggregate class.
Can the annotation “CreationPolicy” be used in such a case, too?

I’m just wondering how Axon finds out what aggregate should be instanciated just by looking at the

TargetAggregateIdentifier

inside the command.

That would be some information that would fit here:
https://docs.axoniq.io/reference-guide/implementing-domain-logic/command-handling/external-command-handler

Hello Herr Manns,

It only makes sense to use @CreationPolicy on the Aggregate (internal) command handler.
Physically, you can place this annotation on the external handler, but it will not make any difference (it will be ignored).

This section title in the ref guide is pointing that this feature is for Aggregate Command handlers only: https://docs.axoniq.io/reference-guide/implementing-domain-logic/command-handling/aggregate#aggregate-command-handler-creation-policy.

Actually, it could be that you can pull your command handler inside the aggregate if the reason to make it external was related to the way on how you create your aggregates. This can improve the design and remove some unneeded code.

Best,
Ivan

Hi Ivan,

thanks for your reply. I think I will pull the command handler inside my aggregate then.

Thanks,
Dirk