コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UncommittedAggregateEvents"/> class.
 /// </summary>
 /// <param name="eventSource">The <see cref="EventSourceId"/> that the Events were applied to.</param>
 /// <param name="aggregateRoot">The <see cref="Artifact"/> representing the type of the Aggregate Root that applied the Event 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>
 /// <param name="events">The <see cref="UncommittedEvent">events</see>.</param>
 public UncommittedAggregateEvents(EventSourceId eventSource, Artifact aggregateRoot, AggregateRootVersion expectedAggregateRootVersion, IReadOnlyList <UncommittedEvent> events)
     : base(events)
 {
     EventSource   = eventSource;
     AggregateRoot = aggregateRoot;
     ExpectedAggregateRootVersion = expectedAggregateRootVersion;
 }
コード例 #2
0
ファイル: AggregateRoots.cs プロジェクト: dolittle/Runtime
    /// <inheritdoc/>
    public Task <AggregateRoot> IncrementVersionFor(
        IClientSessionHandle transaction,
        EventSourceId eventSource,
        ArtifactId aggregateRoot,
        AggregateRootVersion expectedVersion,
        AggregateRootVersion nextVersion,
        CancellationToken cancellationToken)
    {
        _logger.IncrementingVersionForAggregate(aggregateRoot, eventSource);
        ThrowIfNextVersionIsNotGreaterThanExpectedVersion(eventSource, aggregateRoot, expectedVersion, nextVersion);

        if (expectedVersion == AggregateRootVersion.Initial)
        {
            return(WriteFirstAggregateRootDocument(
                       transaction,
                       eventSource,
                       aggregateRoot,
                       expectedVersion,
                       nextVersion,
                       cancellationToken));
        }
        else
        {
            return(UpdateExistingAggregateRootDocument(
                       transaction,
                       eventSource,
                       aggregateRoot,
                       expectedVersion,
                       nextVersion,
                       cancellationToken));
        }
    }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommittedAggregateEvent"/> class.
 /// </summary>
 /// <param name="aggregateRoot">The aggregate root <see cref="Artifact" />.</param>
 /// <param name="aggregateRootVersion">The version of the aggregate root that applied the Event.</param>
 /// <param name="eventLogSequenceNumber">The version of the Event Log the Event was committed to.</param>
 /// <param name="occurred">The <see cref="DateTimeOffset" /> when the Event was committed to the Event Store.</param>
 /// <param name="eventSource">The Event Source that the Event was applied to.</param>
 /// <param name="executionContext">The <see cref="ExecutionContext" />.</param>
 /// <param name="type">The <see cref="Artifact"/> representing the type of the Event.</param>
 /// <param name="isPublic">Whether this Event is public.</param>
 /// <param name="content">The content of the Event represented as a JSON-encoded <see cref="string"/>.</param>
 public CommittedAggregateEvent(
     Artifact aggregateRoot,
     AggregateRootVersion aggregateRootVersion,
     EventLogSequenceNumber eventLogSequenceNumber,
     DateTimeOffset occurred,
     EventSourceId eventSource,
     ExecutionContext executionContext,
     Artifact type,
     bool isPublic,
     string content)
     : base(eventLogSequenceNumber, occurred, eventSource, executionContext, type, isPublic, content)
 {
     AggregateRoot        = aggregateRoot;
     AggregateRootVersion = aggregateRootVersion;
 }
コード例 #4
0
    /// <inheritdoc />
    public async Task UpdateAggregateVersions(IClientSessionHandle session, Commit commit, CancellationToken cancellationToken)
    {
        var tasks = new List <Task>();
        var batchedCommittedAggregateEvents = commit.AggregateEvents.ToArray();

        if (batchedCommittedAggregateEvents.Length == 0)
        {
            return;
        }
        foreach (var committedAggregateEvents in batchedCommittedAggregateEvents)
        {
            var expectedVersion = committedAggregateEvents[0].AggregateRootVersion;
            AggregateRootVersion nextVersion = expectedVersion + (ulong)committedAggregateEvents.Count;
            tasks.Add(_aggregateRoots.IncrementVersionFor(session, committedAggregateEvents.EventSource, committedAggregateEvents.AggregateRoot, expectedVersion, nextVersion, cancellationToken));
        }
        await Task.WhenAll(tasks).ConfigureAwait(false);
    }
コード例 #5
0
 /// <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="ArtifactId"/> representing the type of the Aggregate Root that applied the Event to the Event Source.</param>
 /// <param name="events">The <see cref="CommittedAggregateEvent">events</see>.</param>
 public CommittedAggregateEvents(EventSourceId eventSource, ArtifactId aggregateRoot, IReadOnlyList <CommittedAggregateEvent> events)
     : base(events)
 {
     EventSource   = eventSource;
     AggregateRoot = aggregateRoot;
     for (var i = 0; i < events.Count; i++)
     {
         if (i == 0)
         {
             _currentCheckedVersion = events[0].AggregateRootVersion;
         }
         var @event = events[i];
         ThrowIfEventWasAppliedToOtherEventSource(@event);
         ThrowIfEventWasAppliedByOtherAggregateRoot(@event);
         ThrowIfAggreggateRootVersionIsOutOfOrder(@event);
         _currentCheckedVersion++;
     }
 }
コード例 #6
0
    public static void should_be_the_correct_response(
        this CommitAggregateEventsResponse response,
        UncommittedAggregateEvents uncommitted_events,
        ExecutionContext execution_context,
        EventLogSequenceNumber start_sequence_number      = null,
        AggregateRootVersion start_aggregate_root_version = null)
    {
        var committedEvents = response.Events.ToCommittedEvents();

        committedEvents.Count.ShouldEqual(uncommitted_events.Count);
        if (uncommitted_events.Count == 0)
        {
            return;
        }

        committedEvents.AggregateRoot.ShouldEqual(uncommitted_events.AggregateRoot.Id);
        committedEvents.EventSource.ShouldEqual(uncommitted_events.EventSource);

        for (var i = 0; i < committedEvents.Count; i++)
        {
            var committedEvent   = committedEvents[i];
            var uncommittedEvent = uncommitted_events[i];

            if (start_sequence_number != null)
            {
                committedEvent.EventLogSequenceNumber.ShouldEqual(new EventLogSequenceNumber(start_sequence_number + (ulong)i));
            }
            if (start_aggregate_root_version != null)
            {
                committedEvent.AggregateRootVersion.ShouldEqual(new AggregateRootVersion(start_aggregate_root_version + (ulong)i));
            }

            committedEvent.AggregateRoot.ShouldEqual(uncommitted_events.AggregateRoot);
            committedEvent.ExecutionContext.ShouldEqual(execution_context);
            committedEvent.Content.ShouldEqual(uncommittedEvent.Content);
            committedEvent.Public.ShouldEqual(uncommittedEvent.Public);
            committedEvent.Type.ShouldEqual(uncommittedEvent.Type);
            committedEvent.EventSource.ShouldEqual(uncommittedEvent.EventSource);
        }
    }
コード例 #7
0
ファイル: EventCommitter.cs プロジェクト: srudaa/Runtime
        /// <inheritdoc/>
        public async Task <CommittedAggregateEvent> CommitAggregateEvent(
            IClientSessionHandle transaction,
            Artifact aggregateRoot,
            AggregateRootVersion aggregateRootVersion,
            EventLogSequenceNumber version,
            DateTimeOffset occurred,
            EventSourceId eventSource,
            Execution.ExecutionContext executionContext,
            UncommittedEvent @event,
            CancellationToken cancellationToken)
        {
            await InsertEvent(
                transaction,
                version,
                occurred,
                eventSource,
                @event,
                new AggregateMetadata
            {
                WasAppliedByAggregate = true,
                TypeId         = aggregateRoot.Id,
                TypeGeneration = aggregateRoot.Generation,
                Version        = aggregateRootVersion
            },
                executionContext,
                cancellationToken).ConfigureAwait(false);

            return(new CommittedAggregateEvent(
                       aggregateRoot,
                       aggregateRootVersion,
                       version,
                       occurred,
                       eventSource,
                       executionContext,
                       @event.Type,
                       @event.Public,
                       @event.Content));
        }
コード例 #8
0
 /// <inheritdoc/>
 public async Task <Try <bool> > TryMarkAsRemove(
     EmbeddingId embedding,
     ProjectionKey key,
     AggregateRootVersion version,
     CancellationToken token)
 {
     try
     {
         return(await OnEmbedding(
                    embedding,
                    async collection =>
         {
             var markDefinition = Builders <Embedding>
                                  .Update
                                  .Set(_ => _.IsRemoved, true)
                                  .Set(_ => _.Version, version.Value);
             var markResult = await collection
                              .UpdateOneAsync(
                 CreateKeyFilter(key),
                 markDefinition,
                 new UpdateOptions {
                 IsUpsert = true
             },
                 token)
                              .ConfigureAwait(false);
             return markResult.IsAcknowledged;
         },
                    token).ConfigureAwait(false));
     }
     catch (MongoWaitQueueFullException)
     {
         return(false);
     }
     catch (Exception ex)
     {
         return(ex);
     }
 }
コード例 #9
0
ファイル: events.cs プロジェクト: srudaa/Runtime
 public static MongoDB.Events.StreamEvent an_aggregate_stream_event(StreamPosition stream_position, PartitionId partition, AggregateRootVersion aggregate_version) => new stream_event_builder(stream_position, partition, aggregate_version).build();
コード例 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AggregateRootVersionIsOutOfOrder"/> class.
 /// </summary>
 /// <param name="eventSource">The <see cref="EventSourceId" />.</param>
 /// <param name="aggregateRoot">The aggregate root id.</param>
 /// <param name="eventVersion">The <see cref="AggregateRootVersion"/> the Event was applied by.</param>
 /// <param name="expectedVersion">Expected <see cref="AggregateRootVersion"/>.</param>
 public AggregateRootVersionIsOutOfOrder(EventSourceId eventSource, ArtifactId aggregateRoot, AggregateRootVersion eventVersion, AggregateRootVersion expectedVersion)
     : base($"Aggregate Root version for event source {eventSource} on aggregate root {aggregateRoot} is out of order. Version '{eventVersion}' from event does not match '{expectedVersion}'")
 {
 }
コード例 #11
0
ファイル: events.cs プロジェクト: dolittle/Runtime
 public static event_builder an_aggregate_event_builder(EventLogSequenceNumber event_log_sequence_number, AggregateRootVersion aggregate_version) => new(event_log_sequence_number, aggregate_version);
コード例 #12
0
 /// <summary>
 /// Fast forward to the specified version of the <see cref="EventSourceId">EventSource</see>.
 /// </summary>
 /// <param name="version">Version to fast forward to.</param>
 public void FastForward(AggregateRootVersion version)
 {
     ThrowIfStateful();
     Version = version;
 }
コード例 #13
0
 /// <summary>
 /// Initializes an instance of the <see cref="FailedToReplaceEmbedding" /> class.
 /// </summary>
 /// <param name="embedding">The embedding identifier.</param>
 /// <param name="key">The projection key</param>
 /// <param name="version">The aggregate root version.</param>
 /// <param name="state">The new projection state.</param>
 public FailedToReplaceEmbedding(EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version, ProjectionState state)
     : base($"Failed to replace embedding with id {embedding.Value}, key {key.Value} and aggregate root version {version.Value} with state {state.Value}")
 {
 }
コード例 #14
0
ファイル: committed_events.cs プロジェクト: srudaa/Runtime
 public static CommittedAggregateEvent a_committed_aggregate_event(EventLogSequenceNumber event_log_sequence_number, ArtifactId aggregate, EventSourceId event_source, AggregateRootVersion aggregate_root_version) =>
 new CommittedAggregateEvent(
     new Artifact(aggregate, 1266306380),
     aggregate_root_version,
     event_log_sequence_number,
     new DateTimeOffset(2732723935, TimeSpan.Zero),
     event_source,
     execution_contexts.create(),
     new Artifact(Guid.Parse("2120418a-7869-46b6-9435-09ba6ab9a4cf"), 2010766075),
     false,
     events.some_event_content);
コード例 #15
0
 /// <summary>
 /// Initializes an instance of the <see cref="FailedToRemoveEmbedding" /> class.
 /// </summary>
 /// <param name="embedding">The embedding identifier.</param>
 /// <param name="key">The projection key</param>
 /// <param name="version">The aggregate root version.</param>
 public FailedToRemoveEmbedding(EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version)
     : base($"Failed to remove embedding with id {embedding.Value}, key {key.Value} and aggregate root version {version.Value}")
 {
 }
コード例 #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MissingEventsForExpectedAggregateRootVersion"/> class.
 /// </summary>
 /// <param name="expectedVersion">The expected <see cref="AggregateRootVersion"/>.</param>
 /// <param name="actualVersion">The actual <see cref="AggregateRootVersion"/> produced by the given events.</param>
 public MissingEventsForExpectedAggregateRootVersion(AggregateRootVersion expectedVersion, AggregateRootVersion actualVersion)
     : base($"Events are missing for aggregate root. Expected version '{expectedVersion}', but provided events resulted in '{actualVersion}'.")
 {
 }
コード例 #17
0
 async Task <Try <CommittedAggregateEvents> > TryGetUnprocessedEvents(ProjectionKey key, AggregateRootVersion aggregateRootVersion, CancellationToken cancellationToken)
 {
     try
     {
         return(await _committedEvents.FetchForAggregateAfter(
                    key.Value,
                    _embedding.Value,
                    aggregateRootVersion,
                    cancellationToken).ConfigureAwait(false));
     }
     catch (Exception ex)
     {
         return(ex);
     }
 }
コード例 #18
0
ファイル: committed_events.cs プロジェクト: dolittle/Runtime
 public static CommittedAggregateEvent a_committed_aggregate_event(EventLogSequenceNumber event_log_sequence_number, ArtifactId aggregate, EventSourceId event_source, AggregateRootVersion aggregate_root_version) =>
コード例 #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NextAggregateRootVersionMustBeGreaterThanCurrentVersion"/> class.
 /// </summary>
 /// <param name="currentVersion">The current <see cref="AggregateRootVersion"/> of the aggregate root instance.</param>
 /// <param name="nextVersion">The <see cref="AggregateRootVersion"/> that was attempted to set on the aggregate root instance.</param>
 public NextAggregateRootVersionMustBeGreaterThanCurrentVersion(AggregateRootVersion currentVersion, AggregateRootVersion nextVersion)
     : base($"Next Aggregate Root instance version must be greater than '{currentVersion}', got '{nextVersion}'.")
 {
 }
コード例 #20
0
 /// <summary>
 /// Initializes an instance of the <see cref="EmbeddingIsRemoved" /> class.
 /// </summary>
 /// <param name="embedding">The embedding id.</param>
 /// <param name="key">The key to the embedding.</param>
 /// <param name="version">The <see cref="AggregateRootVersion"/> of the removed embedding.</param>
 public EmbeddingIsRemoved(EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version)
     : base($"Can't get embedding {embedding.Value} with key {key.Value}, as it's already removed. Aggregate root version of the removed embedding is {version.Value}")
 {
 }
コード例 #21
0
 public static AggregateMetadata random_aggregate_metadata_from_aggregate_event_with_version(AggregateRootVersion version) =>
 new AggregateMetadata(
     true,
     Guid.Parse("31e26598-838b-47b1-82e8-a4f8e7085ff8"),
     51434,
     version);
コード例 #22
0
ファイル: events.cs プロジェクト: srudaa/Runtime
 public static MongoDB.Events.Event an_aggregate_event(EventLogSequenceNumber event_log_sequence_number, AggregateRootVersion aggregate_version) => new event_builder(event_log_sequence_number, aggregate_version).build();
コード例 #23
0
ファイル: events.cs プロジェクト: srudaa/Runtime
 public static stream_event_builder an_aggregate_stream_event_builder(StreamPosition stream_position, PartitionId partition, AggregateRootVersion aggregate_version) => new stream_event_builder(stream_position, partition, aggregate_version);
コード例 #24
0
ファイル: LoggerExtensions.cs プロジェクト: dolittle/Runtime
 internal static void ReplacingEmbedding(this ILogger logger, EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version, ProjectionState state)
 => _replacingEmbedding(logger, embedding, key, version, state, null);
 static CommittedAggregateEvent build_committed_event(EventSourceId eventSource, Type aggregateRoot, AggregateRootVersion aggregateRootVersion, EventLogSequenceNumber eventLogSequenceNumber, IEvent @event, ExecutionContext executionContext) =>
 new CommittedAggregateEvent(eventLogSequenceNumber, DateTimeOffset.Now, eventSource, aggregateRoot, aggregateRootVersion, executionContext, @event);
 /// <summary>
 /// Initializes a new instance of the <see cref="NextAggregateRootVersionMustBeGreaterThanCurrentVersion"/> class.
 /// </summary>
 /// <param name="eventSource">The <see cref="EventSourceId" />.</param>
 /// <param name="aggregateRoot">The aggregate root id.</param>
 /// <param name="currentVersion">The current <see cref="AggregateRootVersion"/> of the aggregate root instance.</param>
 /// <param name="nextVersion">The <see cref="AggregateRootVersion"/> that was attempted to set on the aggregate root instance.</param>
 public NextAggregateRootVersionMustBeGreaterThanCurrentVersion(EventSourceId eventSource, ArtifactId aggregateRoot, AggregateRootVersion currentVersion, AggregateRootVersion nextVersion)
     : base($"Next aggregate root version of aggregate root {aggregateRoot} with event source {eventSource} must be greater than '{currentVersion}', but got '{nextVersion}'.")
 {
 }
コード例 #27
0
ファイル: LoggerExtensions.cs プロジェクト: dolittle/Runtime
 internal static void RemovingEmbedding(this ILogger logger, EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version)
 => _removingEmbedding(logger, embedding, key, version, null);
コード例 #28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AggregateRootConcurrencyConflict"/> class.
 /// </summary>
 /// <param name="eventSource">The <see cref="EventSourceId" />.</param>
 /// <param name="aggregateRoot">The aggregate root id.</param>
 /// <param name="currentVersion">The current version before commit.</param>
 /// <param name="commitVersion">The version of the commit that causes a concurrency conflict.</param>
 public AggregateRootConcurrencyConflict(EventSourceId eventSource, ArtifactId aggregateRoot, AggregateRootVersion currentVersion, AggregateRootVersion commitVersion)
     : base($"Tried to commit events to event source {eventSource} on aggregate root {aggregateRoot} with expected aggregate root version {commitVersion}, but current aggregate root version was {currentVersion}. The expected aggregate root version needs to be the same as the current aggregate root version")
 {
 }
コード例 #29
0
ファイル: LoggerExtensions.cs プロジェクト: dolittle/Runtime
 internal static void ErrorRemovingEmbedding(this ILogger logger, EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version, Exception exception)
 => _errorRemovingEmbedding(logger, embedding, key, version, exception);
コード例 #30
0
 /// <inheritdoc/>
 public Task <CommittedAggregateEvents> FetchForAggregateAfter(EventSourceId eventSource, ArtifactId aggregateRoot, AggregateRootVersion after, CancellationToken cancellationToken)
 => DoFetchForAggregate(
     eventSource,
     aggregateRoot,
     defaultFilter => defaultFilter & _eventFilter.Gt(_ => _.Aggregate.Version, after.Value),
     cancellationToken);