In my current project, I have an aggregate without a no-arg constructor (with a custom AggregateFactory registered).
Recently we added an upsert command that either creates or updates an aggregate. For this to work we shifted from using a constructor annotated with @CommandHandler to a method which was additionally annotated with @CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING).
Unfortunately the AggregateAnnotationCommandHandler always assumes a no-arg constructor to be present in this case. It would be nice if the existing AggregateFactory would be used instead (or the ability to register a dedicated factory for this case?).
https://github.com/AxonFramework/AxonFramework/blob/8ea8532578a98aafd15ebb94534d27b2e60fa703/modelling/src/main/java/org/axonframework/modelling/command/AggregateAnnotationCommandHandler.java#L147-L161
P.S. in our case, only the aggregateIdentifier itself is needed to construct the aggregate instance. So our factory completely ignores the firstEvent provided in the constructor.
As an alternative, maybe a more generic firstMessage (command or event) could also be an option? There already exists an DomainEventMessage which extends a regular EventMessage with a method String getAggregateIdentifier();. Maybe something similar could also be done for CommandMessages?
In either case, it would be nice to have more control of how an aggregated is created.
In my current project, I have an aggregate without a no-arg constructor (with a custom
AggregateFactoryregistered).Recently we added an upsert command that either creates or updates an aggregate. For this to work we shifted from using a constructor annotated with
@CommandHandlerto a method which was additionally annotated with@CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING).Unfortunately the
AggregateAnnotationCommandHandleralways assumes a no-arg constructor to be present in this case. It would be nice if the existingAggregateFactorywould be used instead (or the ability to register a dedicated factory for this case?).https://github.com/AxonFramework/AxonFramework/blob/8ea8532578a98aafd15ebb94534d27b2e60fa703/modelling/src/main/java/org/axonframework/modelling/command/AggregateAnnotationCommandHandler.java#L147-L161
P.S. in our case, only the
aggregateIdentifieritself is needed to construct the aggregate instance. So our factory completely ignores thefirstEventprovided in the constructor.As an alternative, maybe a more generic
firstMessage(command or event) could also be an option? There already exists anDomainEventMessagewhich extends a regularEventMessagewith a methodString getAggregateIdentifier();. Maybe something similar could also be done forCommandMessages?In either case, it would be nice to have more control of how an aggregated is created.