Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
![Event Sourced Grain](https://codeopinion.com/wp-content/uploads/2017/11/logo-300x300.png)
Blog Post Series:
- Part 1 – Practical Orleans
- Part 2 – Grains and Silos
- Part 3 – Smart Cache Pattern
- Part 4 – Event Sourced Grain
- Part 5 – EventStore for Grain Persistence
Event Sourced Grain
I’m going to create a typical example using a bank account. There are basically two events we will raise.Deposited
event to indicated that we have deposited money into the account and and Withdrawn
event to that we have taken money out of the account.
Orleans provides a package Microsoft.Orleans.EventSourcing to help raise events from you Grain as well as apply them to the state. From the package, you now can have your grain derive from JournaledGrain<TState, TEvent>.
Grain State
From the example Grain above we are missing our actual grain state, which really a projection so we can keep our current balance. Once an event is Raised the Grain will call the appropriateApply(T evnt)
method in our state.
Persistence
![](https://codeopinion.com/wp-content/uploads/2015/06/Logo-300x73.png)