I have two aggregates :
- User that maintains information of a user (names, email…)
- Rights : that maintains authorizations granted to an user (its has the user identifier and a list of roles)
When a new user is created in the system, I want that its Rights aggregate be also created (with an empty roles list).
For that I want to use the convenient method AggregateLifecycle.createNew that creates a new aggregate from the current aggregate.
I have this code for the constructor of my User aggregate :
`
@CommandHandler
public UserAggregate(CreateUserCommand command) throws Exception {
String userId = command.getUserId();
AggregateLifecycle.apply(new UserCreatedEvent(userId, …));
// We also create aggregate for rights of this user
OpenUserRightsCommand rightsCommand = new OpenUserRightsCommand(userId);
AggregateLifecycle.createNew(RightsAggregate.class, () -> new RightsAggregate(rightsCommand));
}
`
And the corresponding constructor for the RightsAggregate :
@CommandHandler public RightsAggregate(OpenUserRightsCommand command) { AggregateLifecycle.apply(new UserRightsOpenedEvent(command.getUserId())); }
That is very nice.
But… That fails at execution time with the above error message :
`
WARN org.axonframework.commandhandling.gateway.DefaultCommandGateway - Command ‘xxx.CreateUserCommand’ resulted in org.axonframework.common.AxonConfigurationException(Since repository provider is not provided, we cannot spawn a new aggregate for xxx.RightsAggregate)
`
Why do I need a RepositoryProvider as all my aggregates are configured with their repository. Axon already know the repository of each aggregate class.
My configuration is made manually, as we do not have Sprint in our project :
`
private AggregateConfiguration aggregate(Class aggregateType) {
return AggregateConfigurer
.defaultConfiguration(aggregateType)
.configureRepository(c -> repository(aggregateType, c.eventStore()));
}
private Repository repository(Class aggregateType, EventStore eventStore) {
int snapshotTriggerCount = properties.getEventSnapshotTriggerCount();
Snapshotter snapshotter = new AggregateSnapshotter(eventStore, new GenericAggregateFactory<>(aggregateType));
SnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(snapshotter, snapshotTriggerCount);
return new EventSourcingRepository<>(
aggregateType,
eventStore,
snapshotTriggerDefinition);
}
`