private Task EventAppeared(EventStoreCatchUpSubscription _, ResolvedEvent resolvedEvent) { //filter out technical and test events from the $all stream if (resolvedEvent.Event.EventType.StartsWith("$") || resolvedEvent.Event.EventType.StartsWith("eventType") || resolvedEvent.Event.EventType.StartsWith("PersistentConfig1") || resolvedEvent.Event.EventType.StartsWith("Subscription")) { return(Task.CompletedTask); } //Deserialize event to C# object var @event = resolvedEvent.Deserialize(); //tell user which events are being projected Console.WriteLine("Projecting event {0}", @event.GetType().Name); //check which event appeared in the projection and update read model switch (@event) { case Events.UserRegistered e: _items.Add(new UserReadModel() { UserId = e.UserId, Name = e.Name }); break; case Events.UserNameUpdated e: UpdateItem(e.UserId, user => user.Name = e.Name); break; } return(Task.CompletedTask); }
async Task HandleEvent(StreamSubscription streamSubscription, ResolvedEvent resolvedEvent, CancellationToken cancellationToken) { if (resolvedEvent.Event.EventType.StartsWith("$")) { return; } var evt = resolvedEvent.Deserialize(); if (evt == null) { return; } using (var scope = _serviceProvider.CreateScope()) { var publisher = scope.ServiceProvider.GetRequiredService <IPublisher>(); var domainProjectionType = typeof(DomainEventProjection <>).MakeGenericType(evt.GetType()); var domainProjection = Activator.CreateInstance(domainProjectionType, evt) ?? throw new Exception($"Unable to create domain event projection for event {evt.GetType()}"); await publisher.Publish(domainProjection, cancellationToken); _checkpoint.UpdatePosition(resolvedEvent.Event.Position.CommitPosition); await scope.ServiceProvider.GetRequiredService <ICheckpointProvider>() .StoreCheckpoint(_checkpoint, cancellationToken); } }
private Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent) { if (resolvedEvent.Event.EventType.StartsWith("$")) { return(Task.CompletedTask); } var @event = resolvedEvent.Deserialize(); Log.Debug("Projecting event {type}", @event.GetType().Name); switch (@event) { case Events.CalendarCreated e: _calendarDetails.Add(new ReadModels.CalendarDetails { CalendarId = e.CalendarId, Description = e.CalendarDescription, MaximumBookingTimeInMinutes = e.MaximumBookingTimeInMinutes, NumberOfBookings = 0 }); _calendarOverviews.Add(new ReadModels.CalendarOverview { CalendarId = e.CalendarId, Description = e.CalendarDescription }); break; case Events.CalendarDescriptionChanged e: UpdateCalendarDetails(e.CalendarId, c => c.Description = e.NewCalendarDescription); UpdateCalendarOverview(e.CalendarId, c => c.Description = e.NewCalendarDescription); break; case Events.CalendarMaxBookingTimeChanged e: UpdateCalendarDetails(e.CalendarId, c => c.MaximumBookingTimeInMinutes = e.NewMaximumBookingTimeInMinutes); break; case Events.BookingAdded e: UpdateCalendarDetails(e.CalendarId, c => c.NumberOfBookings++); _bookingDetails.Add(new ReadModels.BookingDetails { CalendarId = e.CalendarId, BookingId = e.BookingId, BookedBy = e.BookedBy, StartTime = e.Start, EndTime = e.End }); break; case Events.BookingRemoved e: UpdateCalendarDetails(e.CalendarId, c => c.NumberOfBookings--); _bookingDetails.Remove( _bookingDetails.FirstOrDefault(b => b.BookingId == e.BookingId)); break; } return(Task.CompletedTask); }
private Task EventAppeared(EventStoreCatchUpSubscription _, ResolvedEvent resolvedEvent) { if (resolvedEvent.Event.EventType.StartsWith("$")) { return(Task.CompletedTask); } var @event = resolvedEvent.Deserialize(); Log.Debug($"Projecting event {@event.GetType().Name}"); return(Task.WhenAll(_projections.Select(x => x.Project(@event)))); }
Task Project(EventStoreCatchUpSubscription subscription, ResolvedEvent re) { if (re.Event.EventType.StartsWith("$")) { return(Task.CompletedTask); } var data = re.Deserialize(); return(Task.WhenAll(_projections.Select(x => x.Project(data)))); }
async Task EventAppeared(StreamSubscription _, ResolvedEvent resolvedEvent, CancellationToken c) { if (resolvedEvent.Event.EventType.StartsWith("$")) { return; } var @event = resolvedEvent.Deserialize() !; await Task.WhenAll( _subscriptions.Select(x => x.Project(@event)) ); }
private async Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent) { if (resolvedEvent.Event.EventType.StartsWith("$")) { return; } var @event = resolvedEvent.Deserialize(); log.Debug($"Projecting event: {@event.GetType().Name}"); await Task.WhenAll(projections.Select(projection => projection.Project(@event))); }
//Builds our ReadModels private Task EventAppeared(EventStoreCatchUpSubscription _, ResolvedEvent resolvedEvent) { //This is to filter out all the technical events, alle technical events starts with $ if (resolvedEvent.Event.EventType.StartsWith("$")) { return(Task.CompletedTask); } var @event = resolvedEvent.Deserialize(); Log.Debug("Projecting event {type}", @event.GetType().Name); return(Task.WhenAll(_projections.Select(x => x.Project(@event)))); }
async Task Handler(StreamSubscription sub, ResolvedEvent re, CancellationToken cancellationToken) { if (re.Event.EventType.StartsWith("$")) { return; } var evt = re.Deserialize(); await Task.WhenAll(_projections.Select(x => x.HandleEvent(evt))); _checkpoint.Position = (long?)re.Event.Position.CommitPosition; await _checkpointStore.StoreCheckpoint(_checkpoint, cancellationToken); }
private async Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent) { if (resolvedEvent.Event.EventType.StartsWith("$")) { return; } var @event = resolvedEvent.Deserialize(); Log.Debug("Projecting event {type}", @event.GetType().Name); await Task.WhenAll(_projections.Select(x => x.Project(@event))); await _checkpointStore.StoreCheckpoint(resolvedEvent.OriginalPosition.Value); }
private Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent) { if (resolvedEvent.Event.EventType.StartsWith("$")) { return(Task.CompletedTask); } var @event = resolvedEvent.Deserialize(); _logger.LogInformation($"Processing event {resolvedEvent.ToString()}"); _logger.LogInformation($"Last position processed {this.subscription.LastProcessedPosition.ToString()}"); return(Task.WhenAll(projections.Select( async x => await x.Project(@event)))); }
public async ValueTask <ulong?> Load(string subscriptionId, CancellationToken ct) { var streamName = GetCheckpointStreamName(subscriptionId); var result = eventStoreClient.ReadStreamAsync(Direction.Backwards, streamName, StreamPosition.End, 1, cancellationToken: ct); if (await result.ReadState == ReadState.StreamNotFound) { return(null); } ResolvedEvent? @event = await result.FirstOrDefaultAsync(ct); return(@event?.Deserialize <CheckpointStored>().Position); }
public async Task <T> Load <T, TId>(TId aggregateId) where T : AggregateRoot <TId> { if (aggregateId == null) { throw new ArgumentNullException(nameof(aggregateId)); } var stream = GetStreamName <T, TId>(aggregateId); var aggregate = (T)Activator.CreateInstance(typeof(T), true); var page = await _connection.ReadStreamEventsForwardAsync(stream, 0, 1024, false); aggregate.Load(page.Events.Select( ResolvedEvent => ResolvedEvent.Deserialize()).ToArray()); return(aggregate); }
async Task EventAppeared(StreamSubscription _, ResolvedEvent resolvedEvent, CancellationToken c) { if (resolvedEvent.Event.EventType.StartsWith("$") || resolvedEvent.Event.EventStreamId.Contains("async_command_handler")) { return; } var @event = resolvedEvent.Deserialize(); var metadata = resolvedEvent.DeserializeMetadata(); await Task.WhenAll( _subscriptions.Select(x => x.Project(@event, metadata)) ); await _checkpointStore.StoreCheckpoint( // ReSharper disable once PossibleInvalidOperationException _isAllStream ?resolvedEvent.OriginalPosition !.Value.CommitPosition : resolvedEvent.Event.EventNumber.ToUInt64() ); }
private Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent) { if (resolvedEvent.Event.EventType.StartsWith("$")) { return(Task.CompletedTask); } var @event = resolvedEvent.Deserialize(); Log.Debug($"Projecting event {@event.GetType().Name}"); switch (@event) { case ClassifiedAdCreated e: _items.Add(new ClassifiedAdDetailsViewModel { ClassifiedAdId = e.Id }); break; case ClassifiedAdTitleChanged e: UpdateItem(e.Id, ad => ad.Title = e.Title); break; case ClassifiedAdTextUpdated e: UpdateItem(e.Id, ad => ad.Description = e.Text); break; case ClassifiedAdPriceUpdated e: UpdateItem(e.Id, ad => { ad.Price = e.Price; ad.CurrencyCode = e.CurrencyCode; }); break; } return(Task.CompletedTask); }
private Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolveEvent) { if (resolveEvent.Event.EventType.StartsWith("$")) { return(Task.CompletedTask); } var @event = resolveEvent.Deserialize(); log.Debug("Projecting event {type}", @event.GetType().Name); switch (@event) { case Events.DailyBudgetCreated e: items.Add(new ReadModels.DailyBudgets { DailyBudgtId = e.Id, Name = e.Name, Snapshot = new ReadModels.Snapshots { DailyBudgetId = e.Id }, Expenditures = new List <ReadModels.Expenditures>(), Incomes = new List <ReadModels.Incomes>(), Outgos = new List <ReadModels.Outgos>() }); break; case Events.IncomeAddedToDailyBudget e: UpdateItem(e.DailyBudgetId, budget => { budget.Incomes.Add(new ReadModels.Incomes { DailyBudgetId = e.DailyBudgetId, IncomeId = e.IncomeId, Amount = e.Amount, Description = e.Description }); }); break; case Events.OutgoAddedToDailyBudget e: UpdateItem(e.DailyBudgetId, budget => { budget.Outgos.Add(new ReadModels.Outgos { DailyBudgetId = e.DailyBudgetId, OutgoId = e.OutgoId, Amount = e.Amount, Description = e.Description }); }); break; case Events.ExpenditureAdded e: UpdateItem(e.DailyBudgetId, budget => { budget.Expenditures.Add(new ReadModels.Expenditures { DailyBudgetId = e.DailyBudgetId, ExpenditureId = e.ExpenditureId, Amount = e.Amount, Description = e.Description }); }); break; case Events.IncomeAmountChanged e: UpdateItem(e.DailyBudgetId, budget => { var income = budget.Incomes.FirstOrDefault(i => i.IncomeId == e.IncomeId); if (income != null) { budget.Incomes.Remove(income); income.Amount = e.Amount; budget.Incomes.Add(income); } }); break; case Events.IncomeDescriptionChanged e: UpdateItem(e.DailyBudgetId, budget => { var income = budget.Incomes.FirstOrDefault(i => i.IncomeId == e.IncomeId); if (income != null) { budget.Incomes.Remove(income); income.Description = e.Description; budget.Incomes.Add(income); } }); break; case Events.OutgoAmountChanged e: UpdateItem(e.DailyBudgetId, budget => { var outgo = budget.Outgos.FirstOrDefault(i => i.OutgoId == e.OutgoId); if (outgo != null) { budget.Outgos.Remove(outgo); outgo.Amount = e.Amount; budget.Outgos.Add(outgo); } }); break; case Events.OutgoDescriptionChanged e: UpdateItem(e.DailyBudgetId, budget => { var outgo = budget.Outgos.FirstOrDefault(i => i.OutgoId == e.OutgoId); if (outgo != null) { budget.Outgos.Remove(outgo); outgo.Description = e.Description; budget.Outgos.Add(outgo); } }); break; case Events.ExpenditureAmountChanged e: UpdateItem(e.DailyBudgetId, budget => { var outgo = budget.Expenditures.FirstOrDefault(i => i.ExpenditureId == e.ExpenditureId); if (outgo != null) { budget.Expenditures.Remove(outgo); outgo.Amount = e.Amount; budget.Expenditures.Add(outgo); } }); break; case Events.ExpenditureDescriptionChanged e: UpdateItem(e.DailyBudgetId, budget => { var outgo = budget.Expenditures.FirstOrDefault(i => i.ExpenditureId == e.ExpenditureId); if (outgo != null) { budget.Expenditures.Remove(outgo); outgo.Description = e.Description; budget.Expenditures.Add(outgo); } }); break; case Events.SnapshotChanged e: UpdateItem(e.DailyBudgetId, budget => { budget.Snapshot.Daily = e.Daily; budget.Snapshot.Available = e.Available; budget.Snapshot.SnapshotId = e.SnapshotId; }); break; case Events.PeriodAddedToDailyBudget e: UpdateItem(e.DailyBudgetId, budget => { budget.Start = e.Start; budget.End = e.End; }); break; case Events.PeriodStartChanged e: UpdateItem(e.DailyBudgetId, budget => budget.Start = e.Start); break; case Events.PeriodEndChanged e: UpdateItem(e.DailyBudgetId, budget => budget.End = e.End); break; } return(Task.CompletedTask); }