AllowCreate

Allows creation of a new aggregate instance if one doesn't already exist.

This annotation enables commands to create aggregate instances on-demand when they don't exist. Without this annotation, commands will fail if the target aggregate cannot be found.

Use this annotation when:

  • Commands should be able to initialize new aggregates

  • The system supports lazy aggregate creation

  • Commands represent creation operations that may be retried

Example usage:

@AggregateRoot
@AllowCreate
class UserAggregate(
@AggregateId
val userId: String
) {

@OnCommand
fun createIfNotExists(command: CreateUserCommand): UserCreated {
// This command can create a new user if one doesn't exist
return UserCreated(userId, command.email)
}
}

See also

for marking commands that always create new aggregates

for aggregate root classes