public async Task SaveAsync(TAggregateRoot aggregate, CancellationToken cancellationToken = default) { var stopWatch = new Stopwatch(); stopWatch.Start(); var events = aggregate.GetUncommittedChanges().ToList(); var streamId = aggregate.GetStream(); var aggregateLoadedAtVersion = aggregate.Version; aggregate.MarkChangesAsCommitted(); await _eventStore.AppendEventsToStreamAsync(streamId, events, aggregateLoadedAtVersion, cancellationToken); if (aggregate is ISnapshotableEntity snapshotableAggregate) { var snapshotVersionFrequency = snapshotableAggregate.SnapshotVersionFrequency ?? _eventSourcingOptions?.DefaultSnapshotVersionFrequency ?? new EventSourcingOptions().DefaultSnapshotVersionFrequency; if (aggregate.Version - snapshotableAggregate.SnapshotVersion >= snapshotVersionFrequency) { var(snapshot, snapshotVersion) = snapshotableAggregate.TakeSnapshot(); await _snapshotStore.StoreSnapshotAsync(new SnapshotEnvelope(snapshot, snapshotVersion, streamId), cancellationToken); } } await PublishEventsAsync(events, cancellationToken); stopWatch.Stop(); _logger.LogDebug("EventSourcedRepository.SaveAsync for {AggregateType} took {ElapsedMilliseconds} ms.", typeof(TAggregateRoot).Name, stopWatch.ElapsedMilliseconds); }
private async Task OnAfterSave(IEnumerable <dynamic> changes, CancellationToken cancellationToken = default) { foreach (var @entity in changes) { await _eventStore.AppendEventsToStreamAsync(@entity.Stream, @entity.Events, null, cancellationToken); } }
public async Task AppendEventsToStreamAsync(string stream, IEnumerable <IEvent> events, int?expectedVersion, CancellationToken cancellationToken = default) { var eventList = events.ToList(); await _innerEventStore.AppendEventsToStreamAsync(stream, eventList, expectedVersion, cancellationToken); foreach (var @event in eventList) { await _messageBusPublisher.PublishAsync(@event, cancellationToken, null, _messagingTopicResolver.ResolveTopicName()); } }
public async Task Save <TData>(Instance <TData> instance, CancellationToken cancellationToken = default) where TData : new() { var events = instance.GetUncommittedChanges().ToList(); var effects = instance.GetUncommittedEffects().ToList(); var streamId = instance.GetStream(); var aggregateLoadedAtVersion = instance.Version; instance.MarkChangesAsCommitted(); await _eventStore.AppendEventsToStreamAsync(streamId, events, aggregateLoadedAtVersion, cancellationToken); foreach (var effect in effects) { await _interpreter.Interpret(effect, cancellationToken); } }
public async Task Save(PayAsYouGoAccount payAsYouGoAccount) { var streamName = StreamNameFor(payAsYouGoAccount.Id); await _eventStore.AppendEventsToStreamAsync(streamName, payAsYouGoAccount.Changes); }