In my previous post, I used the
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.
JournaledGrainto create an Event Sourced grain. This enabled us to raise events from within our grain which would be applied to our grain state. Next up, which I’m covering in this post is how to use EventStore for Orleans Grain Persistence. This means when we raise events, they will also be persisted to EventStore. When our grain is activated, we can re-hydrate it by retrieving prior events from an EventStore stream and re-running them in our Grain to get back to current state.
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
EventStoreIf you are unfamiliar with EventStore:
Writing to EventStoreAnytime our grain was calling the
JournaledGrain.RaiseEvent, we want to actually persist that to an EventStore Stream. For my demo, we will have one EventStore stream per instance of an Orleans grain. Meaning each bank account will have one event stream. I’m going to create a new
RaiseEventmethod that will call the base.RaiseEvent and once confirmed they were applied, append them to our EventStore Stream. The additional private static methods are really just helpers for (de)serializing our events from/to json.
Re-hydratingWhen our Grain activates with
OnActivateAsync, this is when we will fetch all the events from our event stream and apply them to our grain. Basically this will be replaying all the events to build our grain state back up to current state.