コード例 #1
0
        private async Task <List <long> > StreamObjectToVersion(DomainObject domainObject, long maxVersion, string identifier)
        {
            // skip streaming entirely if the object is at or above the desired version
            if (domainObject.MetaVersion >= maxVersion)
            {
                return(new List <long>());
            }

            var appliedEvents = new List <long>();

            await _eventStore.ReplayEventsAsStream(
                tuple =>
            {
                var(recordedEvent, domainEvent) = tuple;

                // stop at the designated max-version
                if (recordedEvent.EventNumber > maxVersion)
                {
                    return(false);
                }

                var versionBefore = domainObject.CurrentVersion;

                domainObject.ApplyEvent(new ReplayedEvent
                {
                    UtcTime     = recordedEvent.UtcTime.ToUniversalTime(),
                    Version     = recordedEvent.EventNumber,
                    DomainEvent = domainEvent
                });

                if (domainObject.CurrentVersion > versionBefore)
                {
                    _logger.LogTrace($"applied event {recordedEvent.EventNumber} / '{recordedEvent.EventType}' to '{identifier}'");
                    appliedEvents.Add(recordedEvent.EventNumber);
                }

                return(true);
            }, startIndex : domainObject.MetaVersion);

            _logger.LogDebug($"applied {appliedEvents.Count} events to '{identifier}': {string.Join(", ", appliedEvents)}");
            return(appliedEvents);
        }