Skip to content

Event Sourced Orleans Grain

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.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


Event Sourced GrainI always thought a Event Sourced Orleans Grain would fit a really well as a stateful Aggregate Root.  Take it a step further and have your Grain be event sourced and also encapsulate a projection of current state.  This post is going to setup the basics of how to Raise events and apply them in our Grain state.  I will be covering persisting the events in the next post.

Blog Post Series:

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 appropriate Apply(T evnt) method in our state.

Persistence

Next I’ll be persisting our events to Event Store.  This will include once a grain is activated, to pull the events and replay them to get 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?  I’d love to hear about it in the comments or on Twitter.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


Leave a Reply

Your email address will not be published. Required fields are marked *