Пример #1
0
        /// <inheritdoc />
        public Task AppendToStreamAsync <TIdentity, TAggregate>(
            TIdentity id,
            AggregateConfiguration <TIdentity, TAggregate> configuration,
            long expectedVersion,
            IEnumerable <IPendingEvent> pendingEvents,
            CancellationToken cancellationToken = default
            ) where TAggregate : Aggregate <TIdentity, TAggregate>
        {
            ExpectedVersionValidation.ValidateExpectedVersion(expectedVersion);

            var stream = _streamNameResolver.Stream(id, configuration);

            var storedEvents   = _streams.GetValueOrDefault(stream);
            var currentVersion = storedEvents?.Last().EventNumber;

            var startingVersion = ExpectedVersionValidation
                                  .StartingVersion(expectedVersion, currentVersion, stream);

            if (storedEvents == null)
            {
                storedEvents     = new List <StoredEvent>();
                _streams[stream] = storedEvents;
            }

            if (startingVersion < storedEvents.Count)
            {
                // Validation phase
                ValidateExistingEvents(
                    stream, expectedVersion, pendingEvents, startingVersion, storedEvents, currentVersion
                    );

                // Everything is already committed
            }
            else
            {
                var recordableEvents = ToStoredEvents(stream, pendingEvents, startingVersion);
                foreach (var recordableEvent in recordableEvents)
                {
                    storedEvents.Add(recordableEvent);
                    AddByEventType(recordableEvent);
                    AddByCategory(recordableEvent, configuration);
                }
            }

            return(Task.CompletedTask);
        }
Пример #2
0
        /// <inheritdoc />
        public Task DeleteAggregateAsync <TIdentity, TAggregate>(
            TIdentity id,
            AggregateConfiguration <TIdentity, TAggregate> configuration,
            long expectedVersion,
            CancellationToken cancellationToken = default
            ) where TAggregate : Aggregate <TIdentity, TAggregate>
        {
            var stream = _streamNameResolver.Stream(id, configuration);

            if (_streams.TryGetValue(stream, out var storedEvents))
            {
                var lastEvent = storedEvents.Last();
                if (expectedVersion != lastEvent.EventNumber)
                {
                    throw ExpectedVersionValidation
                          .UnexpectedVersion(stream, expectedVersion, lastEvent.EventNumber);
                }

                var category   = configuration.Name;
                var byCategory = _byCategory[category];

                foreach (var storedEvent in storedEvents)
                {
                    _byEventType[storedEvent.EventType].Remove(storedEvent);
                    byCategory.Remove(storedEvent);
                }
            }
            else if (expectedVersion != ExpectedVersion.Any && expectedVersion != ExpectedVersion.NoStream)
            {
                if (expectedVersion == ExpectedVersion.StreamExists)
                {
                    throw ExpectedVersionValidation.ExpectedStreamToExist(stream);
                }

                throw ExpectedVersionValidation.ExpectedStreamToExist(stream, expectedVersion);
            }

            _streams.Remove(stream);

            return(Task.CompletedTask);
        }