/// <summary>
        /// Initializes a new instance of the <see cref="CommittedAggregateEvents"/> class.
        /// </summary>
        /// <param name="eventSource">The <see cref="EventSourceId"/> that the Events were applied to.</param>
        /// <param name="aggregateRoot">The <see cref="Type"/> of the Aggregate Root that applied the Events to the Event Source.</param>
        /// <param name="events">The <see cref="CommittedAggregateEvent">events</see>.</param>
        public CommittedAggregateEvents(EventSourceId eventSource, Type aggregateRoot, IReadOnlyList <CommittedAggregateEvent> events)
        {
            EventSource   = eventSource;
            AggregateRoot = aggregateRoot;

            for (var i = 0; i < events.Count; i++)
            {
                if (i == 0)
                {
                    _nextAggregateRootVersion = events[0].AggregateRootVersion;
                }
                var @event = events[i];
                ThrowIfEventIsNull(@event);
                ThrowIfEventWasAppliedToOtherEventSource(@event);
                ThrowIfEventWasAppliedByOtherAggregateRoot(@event);
                ThrowIfAggreggateRootVersionIsOutOfOrder(@event);
                if (i > 0)
                {
                    ThrowIfEventLogVersionIsOutOfOrder(@event, events[i - 1]);
                }
                _nextAggregateRootVersion++;
            }

            _events = new NullFreeList <CommittedAggregateEvent>(events);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CommittedAggregateEvent"/> class.
 /// </summary>
 /// <param name="eventLogSequenceNumber">The event log sequence number of the Event.</param>
 /// <param name="occurred">The <see cref="DateTimeOffset" /> when the Event was committed to the Event Store.</param>
 /// <param name="eventSourceId">The <see cref="EventSourceId" /> of the Event.</param>
 /// <param name="aggregateRoot">The <see cref="Type"/> of the Aggregate Root that applied the Event to the Event Source.</param>
 /// <param name="aggregateRootVersion">The version of the <see cref="AggregateRoot"/> that applied the Event.</param>
 /// <param name="executionContext">The <see cref="ExecutionContext"/> in which the Event was committed.</param>
 /// <param name="event">An instance of the Event that was committed to the Event Store.</param>
 public CommittedAggregateEvent(
     EventLogSequenceNumber eventLogSequenceNumber,
     DateTimeOffset occurred,
     EventSourceId eventSourceId,
     Type aggregateRoot,
     AggregateRootVersion aggregateRootVersion,
     ExecutionContext executionContext,
     IEvent @event)
     : base(eventLogSequenceNumber, occurred, eventSourceId, executionContext, @event)
 {
     AggregateRoot        = aggregateRoot;
     AggregateRootVersion = aggregateRootVersion;
 }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventContext"/> class.
 /// </summary>
 /// <param name="sequenceNumber">The <see cref="EventLogSequenceNumber">sequence number</see> that uniquely identifies the event in the event log which it was committed.</param>
 /// <param name="eventSourceId">The <see cref="EventSourceId"/> that the event was committed to.</param>
 /// <param name="occurred">The <see cref="DateTimeOffset"/> when the event was committed to the <see cref="IEventStore"/>.</param>
 /// <param name="executionContext">The <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.</param>
 /// <param name="uniqueIdentifier">The <see cref="EventIdentifier"/> that uniquely identifies the event.</param>
 protected EventContext(
     EventLogSequenceNumber sequenceNumber,
     EventSourceId eventSourceId,
     DateTimeOffset occurred,
     ExecutionContext executionContext,
     EventIdentifier uniqueIdentifier)
 {
     SequenceNumber   = sequenceNumber;
     EventSourceId    = eventSourceId;
     Occurred         = occurred;
     ExecutionContext = executionContext;
     UniqueIdentifier = uniqueIdentifier;
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommittedExternalEvent"/> class.
 /// </summary>
 /// <param name="eventLogSequenceNumber">The event log sequence number of the Event.</param>
 /// <param name="occurred">The <see cref="DateTimeOffset"/> when the Event was committed to the Event Store.</param>
 /// <param name="eventSourceId">The <see cref="EventSourceId"/> of the Event.</param>
 /// <param name="executionContext">The <see cref="ExecutionContext"/> in which the Event was committed.</param>
 /// <param name="externalEventLogSequenceNumber">The event log sequence number of the Event in the event log of the external Microservice that committed the event.</param>
 /// <param name="received">The <see cref="DateTimeOffset"/> when the Event was received over the Event Horizon.</param>
 /// <param name="event">An instance of the Event that was committed to the Event Store.</param>
 public CommittedExternalEvent(
     EventLogSequenceNumber eventLogSequenceNumber,
     DateTimeOffset occurred,
     EventSourceId eventSourceId,
     ExecutionContext executionContext,
     EventLogSequenceNumber externalEventLogSequenceNumber,
     DateTimeOffset received,
     IEvent @event)
     : base(eventLogSequenceNumber, occurred, eventSourceId, executionContext, @event)
 {
     ExternalEventLogSequenceNumber = externalEventLogSequenceNumber;
     Received = received;
 }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommittedEvent"/> class.
 /// </summary>
 /// <param name="eventLogSequenceNumber">The event log sequence number of the Event.</param>
 /// <param name="occurred">The <see cref="DateTimeOffset" /> when the Event was committed to the Event Store.</param>
 /// <param name="eventSourceId">The <see cref="EventSourceId" /> of the Event.</param>
 /// <param name="executionContext">The <see cref="ExecutionContext"/> in which the Event was committed.</param>
 /// <param name="event">An instance of the Event that was committed to the Event Store.</param>
 public CommittedEvent(
     EventLogSequenceNumber eventLogSequenceNumber,
     DateTimeOffset occurred,
     EventSourceId eventSourceId,
     ExecutionContext executionContext,
     IEvent @event)
 {
     EventLogSequenceNumber = eventLogSequenceNumber;
     Occurred         = occurred;
     ExecutionContext = executionContext;
     Event            = @event;
     EventSource      = eventSourceId;
 }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventContext"/> class.
 /// </summary>
 /// <param name="sequenceNumber">The <see cref="EventLogSequenceNumber">sequence number</see> that uniquely identifies the event in the event log which it was committed.</param>
 /// <param name="eventSourceId">The <see cref="EventSourceId"/> that the event was committed to.</param>
 /// <param name="occurred">The <see cref="DateTimeOffset"/> when the event was committed to the <see cref="IEventStore"/>.</param>
 /// <param name="executionContext">The <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.</param>
 public EventContext(
     EventLogSequenceNumber sequenceNumber,
     EventSourceId eventSourceId,
     DateTimeOffset occurred,
     ExecutionContext executionContext)
     : this(
         sequenceNumber,
         eventSourceId,
         occurred,
         executionContext,
         new EventIdentifier(executionContext.Microservice, executionContext.Tenant, sequenceNumber))
 {
 }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ExternalEventContext"/> class.
 /// </summary>
 /// <param name="sequenceNumber">The <see cref="EventLogSequenceNumber">sequence number</see> that uniquely identifies the event in the event log which it was received.</param>
 /// <param name="eventSourceId">The <see cref="EventSourceId"/> that the event was committed to.</param>
 /// <param name="occurred">The <see cref="DateTimeOffset"/> when the event was committed to the <see cref="IEventStore"/>.</param>
 /// <param name="executionContext">The <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.</param>
 /// <param name="externalEventLogSequenceNumber">The <see cref="EventLogSequenceNumber">sequence number</see> that uniquely identifies the event in the event log which it was committed (the external Microservice event log).</param>
 /// <param name="received">The <see cref="DateTimeOffset"/> when the Event was received over the Event Horizon.</param>
 public ExternalEventContext(
     EventLogSequenceNumber sequenceNumber,
     EventSourceId eventSourceId,
     DateTimeOffset occurred,
     ExecutionContext executionContext,
     EventLogSequenceNumber externalEventLogSequenceNumber,
     DateTimeOffset received)
     : base(
         sequenceNumber,
         eventSourceId,
         occurred,
         executionContext,
         new EventIdentifier(executionContext.Microservice, executionContext.Tenant, externalEventLogSequenceNumber))
 {
     ExternalEventLogSequenceNumber = externalEventLogSequenceNumber;
     Received = received;
 }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventSource"/> class.
 /// </summary>
 /// <param name="id"><see cref="EventSourceId"/> of the event source.</param>
 protected EventSource(EventSourceId id)
 {
     EventSourceId     = id;
     UncommittedEvents = new UncommittedEvents(this);
     Version           = EventSourceVersion.Initial;
 }
Example #9
0
 /// <summary>
 /// Initializes an instance of <see cref="EventSource">EventSource</see>
 /// </summary>
 /// <param name="id"><see cref="EventSourceId"/> of the event source</param>
 protected EventSource(EventSourceId id)
 {
     EventSourceId     = id;
     UncommittedEvents = new UncommittedEventStream(this);
 }
Example #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UncommittedAggregateEvents"/> class.
 /// </summary>
 /// <param name="eventSource">The Event Source that the uncommitted events was applied to.</param>
 /// <param name="aggregateRoot">The <see cref="Type"/> of the Aggregate Root that applied the events to the Event Source.</param>
 /// <param name="expectedAggregateRootVersion">The <see cref="AggregateRootVersion"/> of the Aggregate Root that was used to apply the rules that resulted in the Events.</param>
 public UncommittedAggregateEvents(EventSourceId eventSource, Type aggregateRoot, AggregateRootVersion expectedAggregateRootVersion)
 {
     EventSource   = eventSource;
     AggregateRoot = aggregateRoot;
     ExpectedAggregateRootVersion = expectedAggregateRootVersion;
 }
 /// <inheritdoc/>
 public Task <CommittedAggregateEvents> FetchForAggregate <TAggregateRoot>(EventSourceId eventSource, CancellationToken cancellationToken)
     where TAggregateRoot : AggregateRoot
 => FetchForAggregate(typeof(TAggregateRoot), eventSource, cancellationToken);
        /// <inheritdoc/>
        public async Task <CommittedAggregateEvents> FetchForAggregate(Type aggregateRoot, EventSourceId eventSource, CancellationToken cancellationToken)
        {
            _logger.Debug("Fetching events for aggregate");
            var request = new Contracts.FetchForAggregateRequest
            {
                CallContext = GetCurrentCallContext(),
                Aggregate   = new Contracts.Aggregate
                {
                    AggregateRootId = _artifactMap.GetArtifactFor(aggregateRoot).Id.ToProtobuf(),
                    EventSourceId   = eventSource.ToProtobuf(),
                },
            };
            var response = await _eventStoreClient.FetchForAggregateAsync(request, cancellationToken : cancellationToken);

            ThrowIfFailure(response.Failure);
            try
            {
                return(_eventConverter.ToSDK(response.Events));
            }
            catch (CouldNotDeserializeEvent ex)
            {
                throw new CouldNotDeserializeEventFromScope(ScopeId.Default, ex);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="EventWasAppliedToOtherEventSource"/> class.
 /// </summary>
 /// <param name="eventEventSource">The <see cref="EventSourceId"/> the Event was applied to.</param>
 /// <param name="eventSource"><see cref="EventSourceId"/> of the Event Source.</param>
 public EventWasAppliedToOtherEventSource(EventSourceId eventEventSource, EventSourceId eventSource)
     : base($"Event Source '{eventEventSource}' from event does not match with '{eventSource}'.")
 {
 }
Example #14
0
 /// <summary>
 /// Appends an event to the uncommitted sequence.
 /// </summary>
 /// <param name="eventSource">The <see cref="EventSourceId"/> of the <see cref="UncommittedEvent"/> to append.</param>
 /// <param name="event">The <see cref="IEvent"/> of the <see cref="UncommittedEvent"/> to append.</param>
 public void Append(EventSourceId eventSource, IEvent @event)
 => Append(new UncommittedEvent(eventSource, @event));