Beispiel #1
0
        public void AppendEventsToStream(IAggregateIdentity aggregateId, int expectedVersion, IEvent[] eventsToAppend)
        {
            if(!eventsToAppend.Any())
                return;

            var actualVersion = _persistance.GetVersionFor(aggregateId);
            if (actualVersion != expectedVersion)
            {
                var committedEvents = _persistance.GetEventsFor(aggregateId).Skip(expectedVersion).ToList();
                if (_conflictDetector.HasConflict(committedEvents, eventsToAppend))
                    throw new AggregateConcurrencyException(expectedVersion, actualVersion);

                AppendEventsToStream(aggregateId, expectedVersion + committedEvents.Count, eventsToAppend);
                return;
            }

            _persistance.AppendEvents(aggregateId, eventsToAppend);
            _publisher.Publish(eventsToAppend);
        }