I’ve been looking at Event Store for a little while now and one thing that stumped me is when should one consider writing a projection? Is it common practice to create a projection to create a flattened view of events? For example, lets say I have the following two events (you can imagine the model a little more complex than this however for brevity I have slimmed this down for simplicity):
public class OrderPlaced
{
public OrderPlaced(string reference, Currency currency, Amount amount)
{
…
}
public string Reference { get; }
public Currency Currency { get; } //Custom type
public Amount Amount { get; } //Custom type
}
public class OrderCompleted
{
public OrderCompleted(string reference)
{
this.Reference = reference;
}
public string Reference { get; }
}
public class Currency
{
public Currency(string value)
{
this.Value = value;
}
public string Value { get; }
}
public class Amount
{
public Amount(decimal value)
{
this.Value = value;
}
public decimal Value { get; }
}
``
The approach I am using is an "aggregate per stream" and currently I have a projection that aggregates each stream into a single stream using the following:
fromCategory(‘MyCategory’)
.whenAny(function(s, e) {
linkTo(“Foo”, e);
})
``
The above doesn’t do a great deal and this got me thinking. Am I using projections incorrectly, is it better I created a more flattened / de-normalized view of the aggregate instead? For example, I am aware of the emit function and perhaps this is an ideal candidate to make use of it whereby I can convert the above model(s) and project something like this:
{
string Reference;
string CurrencyCode;
decimal TotalPayingAmount;
}
``
Cheers.