/// <inheritdoc/>
        public void Commit(TransactionCorrelationId correlationId, UncommittedEventStream uncommittedEventStream)
        {
            var envelopes        = _eventEnvelopes.CreateFrom(uncommittedEventStream.EventSource, uncommittedEventStream.EventsAndVersion);
            var envelopesAsArray = envelopes.ToArray();
            var eventsAsArray    = uncommittedEventStream.ToArray();

            var eventsAndEnvelopes = new List <EventAndEnvelope>();

            for (var eventIndex = 0; eventIndex < eventsAsArray.Length; eventIndex++)
            {
                var envelope = envelopesAsArray[eventIndex];
                var @event   = eventsAsArray[eventIndex];
                eventsAndEnvelopes.Add(new EventAndEnvelope(
                                           envelope
                                           .WithTransactionCorrelationId(correlationId)
                                           .WithSequenceNumber(_eventSequenceNumbers.Next())
                                           .WithSequenceNumberForEventType(_eventSequenceNumbers.NextForType(envelope.Event)),
                                           @event
                                           ));
            }

            _eventStore.Commit(eventsAndEnvelopes);
            _eventSourceVersions.SetFor(envelopesAsArray[0].EventSource, envelopesAsArray[0].EventSourceId, envelopesAsArray[envelopesAsArray.Length - 1].Version);

            var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId, eventsAndEnvelopes);

            _committedEventStreamSender.Send(committedEventStream);
        }
Beispiel #2
0
 void ValidateEventStream(CommittedEventStream eventStream)
 {
     if (!IsForThisEventSource(eventStream.EventSourceId))
     {
         throw new InvalidOperationException("Cannot apply an EventStream belonging to a different event source." +
                                             string.Format(@"Expected events for Id {0} but got events for Id {1}", EventSourceId, eventStream.EventSourceId));
     }
 }
Beispiel #3
0
        /// <inheritdoc/>
        public virtual void ReApply(CommittedEventStream eventStream)
        {
            ValidateEventStream(eventStream);

            foreach (var eventAndEnvelope in eventStream)
            {
                InvokeOnMethod(eventAndEnvelope.Event);
                Version = eventAndEnvelope.Envelope.Version;
            }

            Version = Version.NextCommit();
        }
        void CommittedEventStreamReceived(CommittedEventStream committedEventStream)
        {
            committedEventStream.ForEach(e =>
            {
                var results = _eventProcessors.Process(e.Envelope, e.Event);
                Parallel.ForEach(results, result =>
                {
                    if (result.Status == EventProcessingStatus.Success)
                    {
                        _eventProcessorStates.ReportSuccessFor(result.EventProcessor, e.Event, e.Envelope);

                        if (result.Messages.Count() > 0)
                        {
                            _eventProcessorLog.Info(result.EventProcessor, e.Event, e.Envelope, result.Messages);
                        }
                    }
                    else
                    {
                        _eventProcessorStates.ReportFailureFor(result.EventProcessor, e.Event, e.Envelope);
                        _eventProcessorLog.Failed(result.EventProcessor, e.Event, e.Envelope, result.Messages);
                    }
                });
            });
        }