Пример #1
0
        /// <inheritdoc/>
        public IEnumerable <EventAndEnvelope> GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId)
        {
            var eventSourceIdentifier = _applicationResourceIdentifierConverter.AsString(eventSource);
            var eventPath             = GetPathFor(eventSourceIdentifier, eventSourceId);

            var files         = _files.GetFilesIn(eventPath).OrderBy(f => f);
            var eventFiles    = files.Where(f => f.EndsWith(".event")).ToArray();
            var envelopeFiles = files.Where(f => f.EndsWith(".envelope")).ToArray();

            if (eventFiles.Length != envelopeFiles.Length)
            {
                throw new Exception($"There is a problem with event files for {eventSourceIdentifier} with Id {eventSourceId}");
            }

            var events = new List <EventAndEnvelope>();

            for (var eventIndex = 0; eventIndex < eventFiles.Length; eventIndex++)
            {
                var envelopeFile = envelopeFiles[eventIndex];
                var eventFile    = eventFiles[eventIndex];

                var envelopeAsJson = _files.ReadString(Path.GetDirectoryName(envelopeFile), Path.GetFileName(envelopeFile));
                var eventAsJson    = _files.ReadString(Path.GetDirectoryName(eventFile), Path.GetFileName(eventFile));
                var envelopeValues = _serializer.GetKeyValuesFromJson(envelopeAsJson);

                var _correllationId             = Guid.Parse((string)envelopeValues["CorrelationId"]);
                var _eventId                    = Guid.Parse((string)envelopeValues["EventId"]);
                var _sequenceNumber             = (long)envelopeValues["SequenceNumber"];
                var _sequenceNumberForEventType = (long)envelopeValues["SequenceNumberForEventType"];
                var _generation                 = (long)envelopeValues["Generation"];
                var _event              = _applicationResourceIdentifierConverter.FromString((string)envelopeValues["Event"]);
                var _eventSourceId      = Guid.Parse((string)envelopeValues["EventSourceId"]);
                var _eventSource        = _applicationResourceIdentifierConverter.FromString((string)envelopeValues["EventSource"]);
                var _eventSourceVersion = EventSourceVersion.FromCombined(double.Parse(envelopeValues["Version"].ToString()));
                var _causedBy           = (string)envelopeValues["CausedBy"];
                var _occurred           = (DateTime)envelopeValues["Occurred"];

                var envelope = new EventEnvelope(
                    _correllationId,
                    _eventId,
                    _sequenceNumber,
                    _sequenceNumberForEventType,
                    (int)_generation,
                    _event,
                    _eventSourceId,
                    _eventSource,
                    _eventSourceVersion,
                    _causedBy,
                    _occurred
                    );

                var eventType = _applicationResourceResolver.Resolve(envelope.Event);
                var @event    = Activator.CreateInstance(eventType, eventSourceId) as IEvent;
                _serializer.FromJson(@event, eventAsJson);
                events.Add(new EventAndEnvelope(envelope, @event));
            }

            return(events);
        }