Creation of aggregate when using Scala

Just tinkering with Scala and Scynapse and I was trying to create the aggregate using a command handling aggregate root. Obviously constructors are a little different in Scala so annotating them with @CommandHandler doesn’t work.

I have the following code;

class Player extends AbstractAnnotatedAggregateRoot[String] {

@AggregateIdentifier
private var id : String = _

@CommandHandler
def handle(cmd: CreatePlayerCommand){
print(cmd)
}

@CommandHandler
def handle(cmd: PlayerGoalScoredCommand){
print(cmd)
}

@EventSourcingHandler
def handle(evt: PlayerCreatedEvent): Unit ={
this.id = evt.getId
}
}

The CreatePlayerCommand is doing the creation and in Java would have been in the constructor args.

Hi David,

It’s possible to create an aggregate like this:

@CommandHandler
def this(cmd: CreatePlayerCommand) = {
this()
apply(PlayerCreatedEvent(cmd.playerId, more values))
}

@EventHandler
def on(e: PlayerCreatedEvent) = {
id = e.getId
}

This works with case classes for the Commands and Events.
@EventSourcingHandler should work too, we can debate which one is better. I did not use EventSourcingHandler, but will give that a try.

Kind regards,

Olger

And the CreatePlayerCommand should have:

case class CreatePlayerCommand(@aggregateId playerId: String, other: String at all…)

In order to resolve the aggregate root.

Kind regards,

Olger