Skip to content

CommandHandler annotated with @CreationPolicy doesn't work if a no-arg constructor is missing #2150

@lion7

Description

@lion7

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.

Metadata

Metadata

Assignees

Labels

Priority 3: CouldLow priority. Issues that are nice to have but have a straightforward workaround.Status: In ProgressUse to signal this issue is actively worked on.Status: ResolvedUse to signal that work on this issue is done.Type: EnhancementUse to signal an issue enhances an already existing feature of the project.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions