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.
Projections are an important yet pretty simple concept when working with event-centric or event sourcing systems. The concept is to build a state from a stream of events. In my previous post, Event Sourcing with SQL Stream Store, I made a pretty basic projection to keep the current account balance. In this post, I’ll use Liquid Projections to accomplish the same task.
I recommend checking out my post on SQL Stream Store as I’m using the same example/demo application in this post.
Liquid Projection is a library for building projections. The concept and API are pretty simple, and I’ll cover the basics in this post.
First I’ll update my Demo Application to use the Liquid Projections NuGet package.
The first building block is creating an event map. The idea is to map an event to an action that we want to be invoked when the event occurs. We can use the EventMapBuild<TContext> to create all of our event mappings. The TContext can be anything but I’ll be using it to mutate the state that represents the current balance.
As a refresher from my previous post, here is the Balance
Now using the EventMapBuilder<Balance> to handle the Deposited and Withdrawn events.
You’ll notice call Build() on the EventMapBuilder<Balance> which will return us the EventMap<Balance>. At this point, you can call Handle() to pass in the events incoming from our subscription with our current Balance.
There are also some nice additions such as providing conditions for when to execute a map. For example, if you wanted to handle only deposited events where the amount was greater than 100.
There is also support for inheritance, which means since my Deposited and Withdrawn events inherit from the AccountEvent base class, I could also have done
Event Store Integration
The next on my list is write this up directly with to something like Event Store or SQL Stream Store. Stay tuned.
Check out Liquid Projections on GitHub for more.
All the source code shown in this post is available on GitHub.
If you’d like a more detailed contented related to various aspects of event sourcing, let me know in the comments or on twitter.