Last active
November 16, 2017 03:52
-
-
Save yoshiyoshifujii/8a4e4832955b1128325f7fd46d873061 to your computer and use it in GitHub Desktop.
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
trait Event[Aggregate] { | |
def applyTo(aggregate: Aggregate): Aggregate | |
} | |
trait Customer { | |
val id: String | |
val version: Long | |
def lockForAccountOverdraft(comment: String): Customer = ??? | |
def locked: Customer | |
val changes: Seq[Event[Customer]] = ??? | |
trait CustomerLocked extends Event[Customer] { | |
override def applyTo(customer: Customer): Customer = | |
customer.locked | |
} | |
def when(customerLocked: CustomerLocked): Customer = ??? | |
} | |
object Customer { | |
def apply(events: Seq[Event[Customer]]): Customer = { | |
events.foldLeft(Customer.empty)((customer, event) => | |
event.applyTo(customer)) | |
} | |
def empty: Customer = ??? | |
} | |
trait EventStream[Aggregate] { | |
val events: Seq[Event[Aggregate]] | |
val version: Long | |
} | |
trait EventStore { | |
def loadEventStream[Aggregate](customerId: String): EventStream[Aggregate] | |
def appendToStream[Aggregate](customerId: String, version: Long, changes: Seq[Event[Aggregate]]) | |
} | |
trait EventStoreImpl extends EventStore { | |
type AggregateType = String | |
type AggregateId = String | |
val dataStore: Map[AggregateType, Map[AggregateId, Any]] | |
override def loadEventStream[Aggregate](customerId: String): EventStream[Aggregate] = { | |
dataStore.get(customerId) | |
} | |
override def appendToStream[Aggregate](customerId: String, version: Long, changes: Seq[Event[Aggregate]]): Unit = ??? | |
} | |
trait ApplicationService { | |
val eventStore: EventStore | |
def lockForAccountOverdraft(customerId: String, comment: String) = { | |
val stream = eventStore.loadEventStream[Customer](customerId) | |
val customer = Customer(stream.events) | |
val updated = customer.lockForAccountOverdraft(comment) | |
eventStore.appendToStream(customerId, stream.version, updated.changes) | |
} | |
} |
Author
yoshiyoshifujii
commented
Nov 9, 2017
- https://ookami86.github.io/event-sourcing-in-practice/#how-eventsourcing-works/04-restoring-objects-5.md
- http://amzn.to/2hkZ1gF
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment