CreateAggregate
Marks a command as one that creates new aggregate instances.
Commands annotated with @CreateAggregate are designated as initialization commands that create new aggregates. These commands are the first commands applied to an aggregate and establish its initial state.
Create commands typically:
Have no version requirements (target version is 0)
Cannot be applied to existing aggregates
Initialize the aggregate's state from scratch
Publish creation events
Example usage:
@CreateAggregate
data class CreateUserCommand(
@AggregateId
val userId: String,
val email: String,
val name: String
)
@AggregateRoot
class UserAggregate {
@OnCommand
fun create(command: CreateUserCommand): UserCreated {
// This is the first command for new users
return UserCreated(command.userId, command.email, command.name)
}
}Content copied to clipboard
See also
for commands that can create aggregates conditionally
for aggregate root classes