Sponsorship is available! If you’d like to sponsor CodeOpinion.com and have your product or service advertised exclusively (no AdSense) on every post, contact me.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 GrainI’m going to create a typical example using a bank account. There are basically two events we will raise.
Depositedevent to indicated that we have deposited money into the account and and
Withdrawnevent 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
Grain StateFrom 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.