/// <summary>
        /// Saves changes to the respective stream
        /// NOTES: Current implementation doesn't support cancellation token
        /// </summary>
        /// <param name="eventsToAdd"></param>
        /// <param name="snapshot"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task <int> SaveChanges(ItemWithType[] eventsToAdd,
                                                        TState snapshot,
                                                        CancellationToken?cancellationToken)
        {
            checked
            {
                CheckDisposedState();
                ConditionalWriteResult writeResult;

                writeResult = await eventStoreConnection.ConditionalAppendToStreamAsync(
                    streamName,
                    this.ConcurrencyId,
                    eventsToAdd.Select(eventObject => eventObject.CreateEventData()))
                              .ConfigureAwait(false);

                StreamAppendHelpers.CheckConditionalWriteResultStatus(writeResult, streamName);

                if (!writeResult.NextExpectedVersion.HasValue)
                {
                    throw new InvalidOperationException("Eventstore data write outcome unexpected. NextExpectedVersion is null");
                }

                await Initialize();

                ConsiderSessionDisposed();
                return((int)writeResult.NextExpectedVersion.Value);
            }
        }
        public async Task <bool> ApplyUpdatesToStorage(IReadOnlyList <Event> updates, int expectedversion)
        {
            expectedversion = expectedversion - 1;
            var eventData = updates.Select(ToEventData).ToArray();
            var result    = await _connection.ConditionalAppendToStreamAsync(_stream, expectedversion, eventData);

            return(result.Status != ConditionalWriteStatus.VersionMismatch);
        }
Exemple #3
0
        private async Task SoftDeleteByEventImpl(TId selector, EventData softDeleteEvent)
        {
            var id = selector.ToString();

            var expectedVersion = await GetLastEventNumber(id);

            var writeResult = await connection.ConditionalAppendToStreamAsync(
                id,
                expectedVersion,
                new[] { softDeleteEvent })
                              .ConfigureAwait(false);

            StreamAppendHelpers.CheckConditionalWriteResultStatus(writeResult, id);
        }
Exemple #4
0
        public Task Append(Guid aggregateId, long expectedAggregateVersion, IEnumerable <IPendingEvent> pendingEvents)
        {
            if (!pendingEvents.Any())
            {
                return(Task.CompletedTask);
            }

            var aggregateType = pendingEvents.First().AggregateType;

            var eventDataList = new List <EventData>();

            foreach (var @event in pendingEvents)
            {
                eventDataList.Add(new EventData(Guid.NewGuid(), @event.EventType, true, Encoding.UTF8.GetBytes(_serializer.Serialize(@event.Data)), Encoding.UTF8.GetBytes(_serializer.Serialize(@event.Metadata))));
            }

            return(_connection.ConditionalAppendToStreamAsync(aggregateType + "-" + aggregateId, expectedAggregateVersion - 1, eventDataList));
        }
Exemple #5
0
        /// <summary>
        /// Saves changes to the respective stream
        /// NOTES: Current implementation doesn't support cancellation token
        /// </summary>
        /// <param name="eventsToAdd"></param>
        /// <param name="snapshot"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task <int> SaveChanges(ItemWithType[] eventsToAdd,
                                                        TState snapshot,
                                                        CancellationToken?cancellationToken)
        {
            checked
            {
                CheckDisposedState();
                ConditionalWriteResult writeResult;

                writeResult = await eventStoreConnection.ConditionalAppendToStreamAsync(
                    streamName,
                    currentVersion,
                    eventsToAdd.Select(eventObject => CreateEventData(eventObject)))
                              .ConfigureAwait(false);

                switch (writeResult.Status)
                {
                case ConditionalWriteStatus.Succeeded:
                    break;

                case ConditionalWriteStatus.VersionMismatch:
                    throw new ConcurrencyException(streamName, null);

                case ConditionalWriteStatus.StreamDeleted:
                    throw new InvalidOperationException($"Stream was deleted. StreamId: {streamName}");

                default:
                    throw new InvalidOperationException($"Unexpected write result: {writeResult.Status}");
                }

                if (!writeResult.NextExpectedVersion.HasValue)
                {
                    throw new InvalidOperationException("Eventstore data write outcome unexpected. NextExpectedVersion is null");
                }

                ConsiderSessionDisposed();
                currentVersion = (int)writeResult.NextExpectedVersion.Value;
                return((int)writeResult.NextExpectedVersion.Value);
            }
        }