Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A projection
public class SessionActivities : IBundle
{
private Projection<List<SessionActivity>> _projection;
public IProjection Build()
{
_projection = Compose<List<SessionActivity>>
.Projection()
.Initialize(() => new List<SessionActivity>())
.When("SessionOpened", (@event, state) =>
{
var activity = new ActivityFactory().CreateSessionActivity(@event);
if (!activity.HasValue)
return;
state.Add(activity.Value);
})
.When("SessionClosed", (@event, state) =>
{
if (!@event.SessionId.HasValue)
return;
var activity = state.SingleOrDefault(a => a.SessionId == @event.SessionId.Value);
if (activity == null)
return;
activity.Completed(@event.TimeStamp);
})
.Build();
return _projection;
}
public ICommand CreateStorageCommand()
{
if (_projection == null)
throw new InvalidOperationException("Cannot prepare for storage. Invalid state.");
return new PersistSessionActivitiesCommand(_projection.State.ToArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment