Пример #1
0
        public async Task PersistEventAsync(object @event)
        {
            if (!UsingEventSourcing)
            {
                throw new Exception("Event cannot be persisted without using Event Sourcing.");
            }

            var persistedEvent = new PersistedEvent(@event, Index + 1);

            await _eventStore.PersistEventAsync(_actorId, persistedEvent.Index, persistedEvent.Data);

            Index++;

            _applyEvent?.Invoke(persistedEvent);

            if (_snapshotStrategy?.ShouldTakeSnapshot(persistedEvent) == true && _getState is not null)
            {
                var persistedSnapshot = new PersistedSnapshot(_getState(), persistedEvent.Index);

                await _snapshotStore.PersistSnapshotAsync(_actorId, persistedSnapshot.Index, persistedSnapshot.State);
            }
        }