This topic has been lingering for a while. It was never forgotten, but just part of a backlog that had some, let's say, higher priority issues on it. But it's time to address it.
The problem I'm primarily facing is the API design for this. Both using Spring Boot autoconfiguration and the AggregateConfigurer, it should be fairly easy (technically) to manage polymorphism. The main thing Axon needs to do, is find all possible implementations for an aggregate class, and register them. Preferably, this would also include the (type of) event that defines an aggregate is of that specific type. In that case, Axon will automatically know which instance to create when sourcing it from events.
Basically, I see globally two options:
1. Define the possible implementations (and their creation event type) in the abstract parent class. On one hand, this creates coupling of a superclass to its implementing classes (which is not 'pure' in OO terms), but on the other hand, it creates a good overview.
Example:
@Aggregate(implementations = {@PolymorphicAggregate(eventType = SomeImplementationCreatedEvent.class, aggregateType = SomeImplementation.class})
public abstract class SomeAggregateSuperClass {
...
}
2. Each subclass declares itself as a specialized implementation.
@Aggregate
public abstract class SomeAggregateSuperClass {
....
}
@PolymorphicAggregate(initialEvent = SomeImplementationCreatedEvent.class)
public class SomeImplementation extends SomeAggregateSuperclass {
}
The super class doesn't need to be abstract. Basically, that type would be created if the creation event is anything else than the ones specified on the subclasses. Also, the initial event isn't necessary if the Aggregate isn't event sourced. In that case, it should have proper JPA annotations to indicate how implementations are stored and retrieved.
I'm hoping to be able to include this in 3.2, although we're on the brink of releasing that version.
Any feedback is welcome.
Cheers,
Allard