-
Notifications
You must be signed in to change notification settings - Fork 10
Aggregators
Jon Sequeira edited this page Feb 21, 2016
·
2 revisions
An aggregator in Alluvial has the responsibility of taking a "projection" (an object of any type you choose) and some data (a batch of objects, all of the same type), updating the initial state of the projection based on the batch, and returning a new object state. It can be implemented in a few ways. The simplest example of an aggregator is a function:
public delegate Task<TProjection> AggregateAsync<TProjection, in TData>(
TProjection initial,
IStreamBatch<TData> batch);
There's also a corresponding interface, which is used throughout Alluvial.
public interface IStreamAggregator<TProjection, in TData>
{
Task<TProjection> Aggregate(TProjection projection, IStreamBatch<TData> events);
}
But since the interface is so simple, writing a class may be more code than you need. The anonymous implementation pattern is often more convenient. Here's an example with an aggregator that does simple totaling of integers:
var aggregator = Aggregator.Create<int, int>((projection, batch) => projection += batch.Sum());