void ThrowIfEventWasAppliedByOtherAggregateRoot(CommittedAggregateEvents events) { if (events.AggregateRoot != GetType()) { throw new EventWasAppliedByOtherAggregateRoot(events.AggregateRoot, GetType()); } }
void ThrowIfEventWasAppliedToOtherEventSource(CommittedAggregateEvents events) { if (events.EventSource != EventSourceId) { throw new EventWasAppliedToOtherEventSource(events.EventSource, EventSourceId); } }
/// <summary> /// Re-apply events from the Event Store. /// </summary> /// <param name="events">Sequence that contains the events to re-apply.</param> public virtual void ReApply(CommittedAggregateEvents events) { ThrowIfEventWasAppliedToOtherEventSource(events); ThrowIfEventWasAppliedByOtherAggregateRoot(events); foreach (var @event in events) { ThrowIfAggreggateRootVersionIsOutOfOrder(@event); InvokeOnMethod(@event.Event); Version++; } }
/// <summary> /// Converts the <see cref="CommittedAggregateEvents" /> to <see cref="Contracts.CommittedAggregateEvents" />s. /// </summary> /// <param name="committedAggregateEvents">The committed events.</param> /// <returns>The converted <see cref="Contracts.CommittedAggregateEvents" />.</returns> public static Contracts.CommittedAggregateEvents ToProtobuf(this CommittedAggregateEvents committedAggregateEvents) { var aggregateRootVersion = committedAggregateEvents.AsEnumerable().LastOrDefault()?.AggregateRootVersion ?? 0; var protobuf = new Contracts.CommittedAggregateEvents { AggregateRootId = committedAggregateEvents.AggregateRoot.ToProtobuf(), EventSourceId = committedAggregateEvents.EventSource.Value, AggregateRootVersion = aggregateRootVersion }; protobuf.Events.AddRange(committedAggregateEvents.Select(_ => _.ToProtobuf())); return(protobuf); }
/// <inheritdoc/> public Task <Partial <EmbeddingCurrentState> > TryProject(EmbeddingCurrentState currentState, CommittedAggregateEvents events, ExecutionContext executionContext, CancellationToken cancellationToken) => TryProject( currentState, new UncommittedEvents(events.Select(_ => new UncommittedEvent(_.EventSource, _.Type, _.Public, _.Content)).ToList()), executionContext, cancellationToken);