Example #1
0
        public CommittedEventStream Commit(UncommittedEventStream uncommittedEventStream)
        {
            var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId);

            committedEventStream.Append(uncommittedEventStream);
            return(committedEventStream);
        }
Example #2
0
#pragma warning disable 1591 // Xml Comments
		public CommittedEventStream Load(Type aggregatedRootType, Guid aggregateId)
        {
            var events = _repository.GetForAggregatedRoot(aggregatedRootType, aggregateId);
            var stream = new CommittedEventStream(aggregateId);
            stream.Append(events);
            return stream;
        }
Example #3
0
        void CommittedEventStreamReceived(CommittedEventStream committedEventStream)
        {
            _logger.Information($"Committing event stream with {committedEventStream.Count} events");
            committedEventStream.ForEach(e =>
            {
                var results = _eventProcessors.Process(e.Envelope, e.Event);
                Parallel.ForEach(results, result =>
                {
                    if (result.Status == EventProcessingStatus.Success)
                    {
                        _logger.Information("Events processed successfully");
                        _eventProcessorStates.ReportSuccessFor(result.EventProcessor, e.Event, e.Envelope);

                        if (result.Messages.Count() > 0)
                        {
                            _eventProcessorLog.Info(result.EventProcessor, e.Event, e.Envelope, result.Messages);
                        }
                    }
                    else
                    {
                        _logger.Error($"Problems processing with {result.EventProcessor.Identifier}");

                        _eventProcessorStates.ReportFailureFor(result.EventProcessor, e.Event, e.Envelope);
                        _eventProcessorLog.Failed(result.EventProcessor, e.Event, e.Envelope, result.Messages);
                    }
                });
            });
        }
Example #4
0
        /// <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);
        }
Example #5
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));
     }
 }
Example #6
0
        public virtual void ReApply(CommittedEventStream eventStream)
        {
            ValidateEventStream(eventStream);

            foreach (var @event in eventStream)
                ReApply(@event);

            Version = Version.NextCommit();
        }
Example #7
0
        public virtual void ReApply(CommittedEventStream eventStream)
        {
            ValidateEventStream(eventStream);

            foreach (var @event in eventStream)
            {
                ReApply(@event);
            }

            Version = Version.NextCommit();
        }
Example #8
0
		public CommittedEventStream Load(Type aggregatedRootType, Guid aggregateId)
		{
			var query = from e in _context.Events
						where e.PartitionKey == aggregateId.ToString()
						select e;

			var events = query.ToArray();
			var convertedEvents = events.Select(Convert);
			var stream = new CommittedEventStream(aggregateId);
			stream.Append(convertedEvents);
			return stream;
		}
Example #9
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();
        }
Example #10
0
        public CommittedEventStream GetForEventSource(EventSource eventSource, Guid eventSourceId)
        {
            var eventSourceType = eventSource.GetType();
            var query = Query.And(
                            Query.EQ("EventSourceId", eventSourceId),
                            Query.EQ("EventSource", eventSourceType.AssemblyQualifiedName)
                        );

            var cursor = _collection.FindAs<BsonDocument>(query);
            var documents = cursor.ToArray();
            var events = ToEvents(documents);
            var stream = new CommittedEventStream(eventSourceId);
            stream.Append(events);
            return stream;
        }
Example #11
0
        public CommittedEventStream Commit(UncommittedEventStream uncommittedEventStream)
        {
            var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId);

            foreach( var @event in uncommittedEventStream )
            {
                var serialized = _serializer.ToJson(@event, SerializationExtensions.SerializationOptions);
                _client
                    .ExecuteStoredProcedureAsync<long>(_insertEventStoredProcedure.SelfLink, serialized)
                    .ContinueWith(t => @event.Id = t.Result)
                    .Wait();

                committedEventStream.Append(@event);
            }

            return committedEventStream;
        }
Example #12
0
        public CommittedEventStream GetForEventSource(EventSource eventSource, Guid eventSourceId)
        {
            using (var session = _documentStore.OpenSession())
            {
                var eventSourceType = eventSource.GetType();

                var events = session.Query<IEvent>()
                                    .Where(
                                        e => e.EventSourceId == eventSourceId &&
                                             e.EventSource == eventSourceType.AssemblyQualifiedName
                                        ).ToArray();

                var stream = new CommittedEventStream(eventSourceId);
                stream.Append(events);
                return stream;
            }
        }
        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);
                    }
                });
            });
        }
Example #14
0
        public CommittedEventStream Commit(UncommittedEventStream uncommittedEventStream)
        {
            var events = uncommittedEventStream.Select(e => e).ToArray();
            var parameters = new List<OracleParameter>();
            var insertStatementBuilder = new StringBuilder();

            insertStatementBuilder.Append("BEGIN ");
            for (var position = 0; position < events.Length; position++)
            {
                insertStatementBuilder.Append(GetParameterizedInsertStatement(position));
                parameters.AddRange(_eventParameters.BuildFromEvent(position, events[position]));
            }
            insertStatementBuilder.Append(" END;");

            Tuple<int, long>[] returnedIds;

            try
            {
                OpenConnection();
                var transaction = _connection.BeginTransaction();
                try
                {
                    using (var command = _connection.CreateCommand())
                    {
                        command.Transaction = transaction;
                        command.CommandText = insertStatementBuilder.ToString();
                        command.Parameters.AddRange(parameters.ToArray());
                        command.BindByName = true;
                        command.ExecuteNonQuery();

                        returnedIds = ExtractReturnedIds(parameters);
                        transaction.Commit();
                    }
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
            finally
            {
                EnsureConnectionClosed(_connection);
            }

            PopulateEventId(returnedIds, events);

            var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId);
            if (events.Any())
                committedEventStream.Append(events);
            return committedEventStream;
        }
Example #15
0
        public CommittedEventStream GetForEventSource(EventSource eventSource, Guid eventSourceId)
        {
            var committedEventStream = new CommittedEventStream(eventSourceId);

            var eventSourceParam = new OracleParameter(EventParameters.EVENTSOURCE, OracleDbType.NVarchar2, 512);
            eventSourceParam.Value = eventSource.GetType().AssemblyQualifiedName;

            var eventSourceIdParam = new OracleParameter(EventParameters.EVENTSOURCEID, OracleDbType.Raw, 16);
            eventSourceIdParam.Value = eventSourceId.ToByteArray();

            var eventDtos = new List<EventDto>();

            try
            {
                OpenConnection();
                using (var command = _connection.CreateCommand())
                {
                    command.CommandText = READ_STATEMENT_FOR_EVENTS_BY_AGGREGATE_ROOT;
                    command.Parameters.Add(eventSourceIdParam);
                    command.Parameters.Add(eventSourceParam);
                    command.BindByName = true;
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        eventDtos.Add(MapReaderToEventDto(reader));
                    }
                }
            }
            finally
            {
                EnsureConnectionClosed(_connection);
            }

            var retrievedEvents = eventDtos.Select(BuildEventInstanceFromDto)
                                    .Select(@event => _eventMigratorManager.Migrate(@event))
                                    .ToList();

            if (retrievedEvents.Any())
                committedEventStream.Append(retrievedEvents);

            return committedEventStream;
        }
Example #16
0
        public CommittedEventStream Commit(UncommittedEventStream uncommittedEventStream)
        {
            using (var session = _documentStore.OpenSession())
            {
                var eventArray = uncommittedEventStream.ToArray();
                for (var eventIndex = 0; eventIndex < eventArray.Length; eventIndex++)
                {
                    var @event = eventArray[eventIndex];
                    //session.Advanced.
                    session.Store(@event);
                }

                session.SaveChanges();
                var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId);
                committedEventStream.Append(uncommittedEventStream);
                return committedEventStream;
            }
        }
Example #17
0
 public CommittedEventStream Commit(UncommittedEventStream uncommittedEventStream)
 {
     var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId);
     committedEventStream.Append(uncommittedEventStream);
     return committedEventStream;
 }
Example #18
0
        public CommittedEventStream Commit(UncommittedEventStream uncommittedEventStream)
        {
            var eventArray = uncommittedEventStream.ToArray();
            for (var eventIndex = 0; eventIndex < eventArray.Length; eventIndex++)
            {
                var @event = eventArray[eventIndex];
                @event.Id = GetNextEventId();
                var eventDocument = @event.ToBsonDocument();
                AddMetaData(@event, eventDocument);
                _collection.Insert(eventDocument);
            }

            var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId);
            committedEventStream.Append(uncommittedEventStream);
            return committedEventStream;
        }
Example #19
0
 public override void ReApply(CommittedEventStream eventStream)
 {
     ReApplyCalled = true;
     EventStreamApplied = eventStream;
     base.ReApply(eventStream);
 }