This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for dealing with side effects
- Loading branch information
1 parent
ed7f2d0
commit 7b56f37
Showing
14 changed files
with
106 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
code-samples/src/main/scala/event_sourcing/BankAccountRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package event_sourcing | ||
|
||
import java.util.UUID | ||
|
||
trait BankAccountRepository { | ||
def getAccount(id:UUID) : BankAccount | ||
def saveAccount(account:BankAccount) | ||
} |
12 changes: 9 additions & 3 deletions
12
code-samples/src/main/scala/event_sourcing/ChangeOwner.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package event_sourcing | ||
|
||
import java.util.UUID | ||
|
||
// changeOwnerCommand | ||
class ChangeOwner(newOwner:String) extends Command[BankAccount] { | ||
def execute(bankAccount:BankAccount): BankAccount = { | ||
bankAccount.changeOwner(newOwner) | ||
class BankAccountCommandHandler(bankAccountRepo:BankAccountRepository) { | ||
def handle(changeOwner:ChangeOwner) { | ||
val account = bankAccountRepo.getAccount(changeOwner.accountID) | ||
val modifiedAccount = account.changeOwner(changeOwner.newOwner) | ||
bankAccountRepo.saveAccount(modifiedAccount) | ||
} | ||
} | ||
|
||
case class ChangeOwner(accountID: UUID, newOwner:String) extends Command | ||
// changeOwnerCommand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
package event_sourcing | ||
|
||
trait Command[T] { | ||
def execute(someObject:T) : T | ||
} | ||
trait Command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package event_sourcing | ||
|
||
import event_sourcing.events.DomainEvent | ||
|
||
object EventBus { | ||
var eventStream = Seq.empty[DomainEvent] | ||
|
||
def publishEvent(event:DomainEvent) : Unit = { | ||
eventStream :+= event | ||
print(event + " published") | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
code-samples/src/main/scala/event_sourcing/OrderExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package event_sourcing | ||
|
||
import java.util.UUID | ||
import event_sourcing.events.DomainEvent | ||
|
||
abstract class Order | ||
case class BuyOrder(amount:MonetaryAmount, isin:String) extends Order | ||
case class PerformBuyOrder(orderBookId:UUID, order:BuyOrder) extends Command | ||
case class OrderConfirmation(orderId:UUID, order:BuyOrder) | ||
case class OrderSentToBank(orderId:UUID, order:BuyOrder) extends DomainEvent | ||
object OrderSentToBank { | ||
def from(orderConfirmation:OrderConfirmation) = { | ||
OrderSentToBank(orderConfirmation.orderId, orderConfirmation.order) | ||
} | ||
} | ||
|
||
// dealingWithSideEffects | ||
|
||
class BrokerCommandHandler (bankInterface:BankInterface, | ||
orderBookRepository:OrderBookRepository) { | ||
def handle(buyCommand:PerformBuyOrder) = { | ||
var orderBook = orderBookRepository.load(buyCommand.orderBookId) | ||
val validationResult = bankInterface.validate(buyCommand.order) | ||
if (!validationResult.succeeded) | ||
throw new InvalidOrderException(validationResult) | ||
val orderConfirmation = bankInterface.perform(buyCommand.order) | ||
val orderPerformedEvent = OrderSentToBank.from(orderConfirmation) | ||
orderBook = orderBook.handle(orderPerformedEvent) | ||
orderBookRepository.save(orderBook) | ||
} | ||
} | ||
|
||
case class OrderBook(orders:Seq[Order] = Seq.empty) { | ||
def handle(orderSentToBank:OrderSentToBank) : OrderBook = { | ||
this.copy(orders :+ orderSentToBank.order) | ||
} | ||
} | ||
|
||
// dealingWithSideEffects | ||
|
||
trait BankInterface { | ||
def validate(order:BuyOrder) : ValidationResult | ||
def perform(order:BuyOrder) : OrderConfirmation | ||
} | ||
|
||
class InvalidOrderException(validationResult:ValidationResult) extends RuntimeException(validationResult.toString) | ||
|
||
case class ValidationResult(succeeded:Boolean) | ||
case class OrderFailedEvent(validationResult:ValidationResult) extends DomainEvent | ||
|
||
trait OrderBookRepository { | ||
def load(orderBookID:UUID) : OrderBook | ||
def save(orderBook:OrderBook) | ||
} |
5 changes: 3 additions & 2 deletions
5
...ala/event_sourcing/DepositPerformed.scala → ...nt_sourcing/events/DepositPerformed.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package event_sourcing | ||
package event_sourcing.events | ||
|
||
import java.util.UUID | ||
import event_sourcing.MonetaryAmount | ||
|
||
// depositEvent | ||
case class DepositPerformed(account:UUID, amount:MonetaryAmount) | ||
case class DepositPerformed(account:UUID, amount:MonetaryAmount) extends DomainEvent | ||
// depositEvent |
3 changes: 3 additions & 0 deletions
3
code-samples/src/main/scala/event_sourcing/events/DomainEvent.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package event_sourcing.events | ||
|
||
trait DomainEvent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
|
||
### Contra {.cons} | ||
|
||
- | ||
- Replaying may inflict unexpected side effects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## Dealing with side effects | ||
|
||
How do we deal with side effects? | ||
|
||
- Leave side effect to the command handler | ||
{.slide} | ||
- Record result as domain event, if necessary | ||
{.slide} |
1 change: 1 addition & 0 deletions
1
pages/slides/06-how-eventsourcing-works/11-side-effects-example.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@(dealingWithSideEffects)[../../../code-samples/src/main/scala/event_sourcing/OrderExample.scala] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
div.codehilite { | ||
line-height: 1; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters