Esempio n. 1
0
        /// <inheritdoc/>
        public override async Task <Contracts.CommitAggregateEventsResponse> CommitForAggregate(Contracts.CommitAggregateEventsRequest request, ServerCallContext context)
        {
            _executionContextManager.CurrentFor(request.CallContext.ExecutionContext);
            var response = new Contracts.CommitAggregateEventsResponse();

            try
            {
                _logger.Debug("{eventsCount} Aggregate Events received for committing", request.Events.Events.Count);
                var eventSourceId = request.Events.EventSourceId.To <EventSourceId>();
                var events        = request.Events.Events.Select(_ => new UncommittedEvent(eventSourceId, new Artifact(_.Artifact.Id.To <ArtifactId>(), _.Artifact.Generation), _.Public, _.Content));
                var aggregateRoot = new Artifact(request.Events.AggregateRootId.To <ArtifactId>(), ArtifactGeneration.First);

                var uncommittedAggregateEvents = new UncommittedAggregateEvents(
                    eventSourceId,
                    aggregateRoot,
                    request.Events.ExpectedAggregateRootVersion,
                    new ReadOnlyCollection <UncommittedEvent>(events.ToList()));
                var committedEventsResult = await _eventStoreFactory().CommitAggregateEvents(uncommittedAggregateEvents, context.CancellationToken).ConfigureAwait(false);

                _logger.Debug("Aggregate Events were successfully committed");
                response.Events = committedEventsResult.ToProtobuf();
            }
            catch (Exception ex)
            {
                _logger.Warning(ex, "Error committing aggregate events");
                response.Failure = GetFailureFromException(ex);
            }

            return(response);
        }
Esempio n. 2
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);
        }
    }
    /// <summary>
    /// Creates an <see cref="CommitAggregateEventsRequest"/> from the given <see cref="UncommittedAggregateEvents"/> and <see cref="ExecutionContext"/>.
    /// </summary>
    /// <param name="events">The <see cref="UncommittedAggregateEvents"/>.</param>
    /// <param name="executionContext">The <see cref="ExecutionContext"/>.</param>
    /// <returns>The <see cref="CommitAggregateEventsRequest"/>.</returns>
    public static CommitAggregateEventsRequest ToCommitRequest(this UncommittedAggregateEvents events, ExecutionContext executionContext)
    {
        var request = new CommitAggregateEventsRequest
        {
            CallContext = new CallRequestContext
            {
                ExecutionContext = executionContext.ToProtobuf()
            },
            Events = new Events.Contracts.UncommittedAggregateEvents
            {
                AggregateRootId = events.AggregateRoot.Id.ToProtobuf(),
                EventSourceId   = events.EventSource,
                ExpectedAggregateRootVersion = events.ExpectedAggregateRootVersion,
            }
        };

        request.Events.Events.AddRange(events.Select(_ => new Events.Contracts.UncommittedAggregateEvents.Types.UncommittedAggregateEvent
        {
            Content   = _.Content,
            Public    = _.Public,
            EventType = _.Type.ToProtobuf()
        }));
        return(request);
    }
Esempio n. 4
0
    public static async Task <CommitAggregateEventsResponse> Commit(this IEventStore eventStore, UncommittedAggregateEvents events, Dolittle.Runtime.Execution.ExecutionContext executionContext)
    {
        var response = await eventStore.CommitAggregateEvents(events.ToCommitRequest(executionContext), CancellationToken.None);

        return(response);
    }