public override ValueTask ApplyChangesAsync(DocumentSessionBase session, EventSlice <CustomAggregate, int> slice, CancellationToken cancellation, ProjectionLifecycle lifecycle = ProjectionLifecycle.Inline) { var aggregate = slice.Aggregate ?? new CustomAggregate { Id = slice.Id }; foreach (var @event in slice.Events()) { if (@event.Data is CustomEvent e) { switch (e.Letter) { case 'a': aggregate.ACount++; break; case 'b': aggregate.BCount++; break; case 'c': aggregate.CCount++; break; case 'd': aggregate.DCount++; break; } } } session.Store(aggregate); return(new ValueTask()); }
private async Task applyProjectionsAsync(DocumentSessionBase session, ICollection <EventProjection> projections, IEnumerable <TView> views) { var idAssigner = session.Tenant.IdAssignmentFor <TView>(); var resolver = session.Tenant.StorageFor <TView>(); var viewMap = views.ToDictionary(view => (TId)resolver.IdentityFor(view), view => view); foreach (var eventProjection in projections) { var viewId = eventProjection.ViewId; var hasExistingView = viewMap.TryGetValue(viewId, out var view); if (!hasExistingView) { if (eventProjection.Type == ProjectionEventType.CreateAndUpdate) { view = newView(session.Tenant, idAssigner, viewId); viewMap.Add(viewId, view); hasExistingView = true; } } if (eventProjection.Type == ProjectionEventType.CreateAndUpdate || (eventProjection.Type == ProjectionEventType.UpdateOnly && hasExistingView)) { session.Store(view); eventProjection.ProjectTo(session, view).Wait(); } else if (eventProjection.Type == ProjectionEventType.Delete && hasExistingView && await eventProjection.ShouldDelete(session, view)) { session.Delete(view); } } }