EventStore for Orleans Grain Persistence  - orleanseventstore 298x300 - EventStore for Orleans Grain PersistenceIn my previous post, I used the JournaledGrain to create an Event Sourced .  This enabled us to raise events from within our which would be applied to our state.  Next up, which I’m covering in this post is how to use EventStore for Grain .

This means when we raise events, they will also be persisted to .  When our grain is activated, we can re-hydrate it by retrieving prior events from an stream and re-running them in our Grain to get back to current state.

Blog Post Series:

EventStore

If you are unfamiliar with EventStore:

The open-source, functional database with Complex Event Processing in JavaScript.

If you don’t have a running instance, the easiest way is probably with Docker.  Pull down the latest image from docker hub and  as single node running.

docker pull eventstore/eventstore

docker run –name eventstore-node -it -p 211:211 -p 111:111 eventstore/eventstore

There is a .NETStandard 2.0 compatible client that I will be using in our Grain Project.

Install-Package EventStore.ClientAPI.NetCore

Writing to EventStore

Anytime 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 RaiseEvent method that will call the base.RaiseEvent and once confirmed they were applied, append them to our EventStore Stream.  The additional static methods are really just helpers for (de)serializing our events from/to json.

Re-hydrating

When 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.

 

More!

If you want to try the demo, all the source is available on my PracticalOrleans GitHub Repo.

Do you have any questions or comments? Are you using Orleans or EventStore?  I’d love to hear about it in the comments or on Twitter.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here