Slide 60
Slide 60 text
def behavior(num: ProductNumber): Behavior[Product] = {
// DSL to build a Behavior using PF
behaviorFor[Product].whenConstructing { it =>
it.emitsEvent { // PF (Command) => Event
case cmd: CreateProduct if cmd.price > 0 =>
ProductCreated(cmd.name, cmd.description, cmd.price)
}
it.acceptsEvents { // PF (Event) => Aggregate
case evt: ProductCreated =>
Product(evt.name, evt.description, evt.price, num)
}
}.whenUpdating { it =>
it.rejectsCommands { // PF (Aggregate, Command) => Throwable
case (prod, cmd: ChangePrice) if cmd.price < prod.price =>
new CommandException("Can't decrease the price")
}
it.emitsSingleEvent { // PF (Aggregate, Command) => Event
case (_, cmd: ChangePrice) if cmd.price > 0 =>
PriceChanged(cmd.price)
}