public void SaveAggregate(IAggregateRoot aggregate)
        {
            var expectedVersion = aggregate.Version;

            //get aggregate version by id
            var version = eventStore.GetVersionByAggregate(aggregate.Id);

            //check consistency...
            if (version.HasValue && version != expectedVersion)
            {
                throw new ConsistencyException(aggregate.Id);
            }

            var newVersion = expectedVersion.HasValue ? expectedVersion : 0;

            foreach (var @event in aggregate.GetUncommittedChanges())
            {
                newVersion++;
                @event.Version = newVersion.Value;
                eventStore.Save(@event);
                eventBus.PublishEvent(@event);
            }

            aggregate.MarkChangesAsCommitted();
        }
Beispiel #2
0
        /// <summary>
        /// Save all changes
        /// </summary>
        /// <param name="aggregate"></param>
        /// <param name="handleEventsSynchronously"></param>
        public async Task SaveAsync(IAggregateRoot aggregate)
        {
            await _eventStore.SaveEventsAsync(aggregate.Id, aggregate.CurrentVersion, aggregate.GetUncommittedChanges());

            aggregate.MarkChangesAsCommitted();
        }