I 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:
- 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
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.
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.Follow @codeopinion