private TAggregateRoot Get(Guid id, int?version)
        {
            var aggregateId   = EventStreamIdFormatter.GetStreamId <TAggregateRoot>(id.ToString());
            var entityVersion = version ?? EntityVersion.Latest;

            _logger.Debug(this, $"Loading stream {aggregateId}@{EntityVersion.GetName(entityVersion)}");

            var stream = _eventStore.Load <TAggregateRoot>(aggregateId, entityVersion);

            if (stream == null)
            {
                _logger.Warn(this, "No stream found for ID {0}", aggregateId);
                return(null);
            }

            if (!stream.HasEvents)
            {
                _logger.Warn(this, "Stream for ID {0} has no events for version {1}", aggregateId, EntityVersion.GetName(entityVersion));
            }

            var aggregateRoot = new TAggregateRoot();

            aggregateRoot.ApplyAll(stream.Events);

            return(aggregateRoot);
        }
Beispiel #2
0
        private TAggregateRoot Get(Guid id, int?version)
        {
            var aggregateId = EventStreamIdFormatter.GetStreamId <TAggregateRoot>(id.ToString());

            _logger.Debug(this, "Loading stream with ID {0} at version {1}", aggregateId, version);

            var stream =
                version == null
                    ? _eventStore.Load <TAggregateRoot>(aggregateId)
                    : _eventStore.Load <TAggregateRoot>(aggregateId, version.Value);

            if (stream == null)
            {
                _logger.Warn(this, "No stream found for ID {0}", aggregateId);
                return(null);
            }

            if (!stream.HasEvents)
            {
                _logger.Warn(this, "Stream for ID {0} has no events for version {1}", aggregateId, version);
            }

            var aggregateRoot = new TAggregateRoot();

            aggregateRoot.ApplyAll(stream.Events);

            return(aggregateRoot);
        }
        public void Save(TAggregateRoot item)
        {
            var uncommittedEvents = item.GetUncommittedEvents().ToArray();

            if (uncommittedEvents.Length == 0)
            {
                return;
            }

            var streamId = EventStreamIdFormatter.GetStreamId <TAggregateRoot>(item.Id.ToString());

            try
            {
                _eventStore.Save <TAggregateRoot>(streamId, item.BaseVersion, uncommittedEvents);
            }
            catch (EventStoreConcurrencyException)
            {
                EventStream <TAggregateRoot> stream = _eventStore.Load <TAggregateRoot>(streamId);

                if (stream != null)
                {
                    IEvent[] baseAndUnseenEvents = stream.Events.ToArray();

                    if (baseAndUnseenEvents.Length > 0)
                    {
                        var unseenEvents = GetUnseenEvents(item, baseAndUnseenEvents);
                        var conflicts    = _concurrencyMonitor.CheckForConflicts(unseenEvents, uncommittedEvents);
                        if (!conflicts.Any())
                        {
                            // Re-version our uncommitted events on top of those already saved to the db.
                            int unseenVersion = unseenEvents.Last().Version;
                            int newVersion    = unseenVersion;

                            foreach (var evt in uncommittedEvents)
                            {
                                evt.Version = ++newVersion;
                            }

                            _eventStore.Save <TAggregateRoot>(streamId, unseenVersion, uncommittedEvents);
                        }
                        else
                        {
                            var exception = new ConcurrencyConflictsDetectedException(conflicts);
                            exception.Data.Add("AggregateId", item.Id);
                            throw exception;
                        }
                    }
                }
            }

            item.AcceptUncommittedEvents();
        }