コード例 #1
0
        public void SaveEvents(AggregateRoot eventSourced, int expectedVersion)
        {
            List<EventDescriptor> eventDescriptors;
            if (!_store.TryGetValue(eventSourced.Id, out eventDescriptors))
            {
                eventDescriptors = new List<EventDescriptor>();
                _store.Add(eventSourced.Id, eventDescriptors);
            }

            else if (eventDescriptors.Last().Version != expectedVersion && expectedVersion != -1)
                throw new ConcurrencyException(String.Format("The aggregate has changed since you started. Expected version {0}, Current Version {1}", expectedVersion, eventDescriptors.Last().Version));

            var i = expectedVersion;
            foreach (var @event in eventSourced.GetUncommitedChanges())
            {
                i++;
                @event.Version = i;
                eventDescriptors.Add(new EventDescriptor(eventSourced.Id, @event, i));
                //Now that the event has actually been stored, let everyone else know.
                _eventBus.Publish(@event);
            }
        }