Пример #1
0
    public void IterationSetup()
    {
        var eventSource   = new EventSourceId("46c4de33-9a60-4465-97ab-a2a7f5b7e6a3");
        var aggregateRoot = new Artifact(new ArtifactId(Guid.NewGuid()), ArtifactGeneration.First);

        _eventsToCommit = new UncommittedAggregateEvents(
            eventSource,
            aggregateRoot,
            AggregateRootVersion.Initial + (ulong)PreExistingEvents,
            _eventsToCommit);

        if (PreExistingEvents > 0)
        {
            var events = new List <UncommittedEvent>();
            for (var n = 0; n < PreExistingEvents; n++)
            {
                events.Add(new UncommittedEvent(
                               new EventSourceId("46c4de33-9a60-4465-97ab-a2a7f5b7e6a3"),
                               new Artifact(new ArtifactId(Guid.Parse("08db4b0a-3724-444f-9968-ada44922fb78")), ArtifactGeneration.First),
                               false,
                               "{ \"hello\": \"world\" }"));
            }
            var preExistingEvents = new UncommittedAggregateEvents(
                eventSource,
                aggregateRoot,
                AggregateRootVersion.Initial,
                events);

            _eventStore.Commit(preExistingEvents, _executionContext).GetAwaiter().GetResult();
        }
    }
Пример #2
0
        /// <inheritdoc/>
        public async Task <CommittedAggregateEvents> CommitAggregateEvents(UncommittedAggregateEvents events, CancellationToken cancellationToken)
        {
            ThrowIfNoEventsToCommit(events);
            try
            {
                using var session = await _streams.StartSessionAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                return(await session.WithTransactionAsync(
                           async (transaction, cancel) =>
                {
                    var eventLogSequenceNumber = (ulong)await _streams.DefaultEventLog.CountDocumentsAsync(
                        transaction,
                        _eventFilter.Empty,
                        cancellationToken: cancel).ConfigureAwait(false);
                    var aggregateRootVersion = events.ExpectedAggregateRootVersion.Value;

                    var committedEvents = new List <CommittedAggregateEvent>();

                    foreach (var @event in events)
                    {
                        var committedEvent = await _eventCommitter.CommitAggregateEvent(
                            transaction,
                            events.AggregateRoot,
                            aggregateRootVersion,
                            eventLogSequenceNumber,
                            DateTimeOffset.UtcNow,
                            events.EventSource,
                            _executionContextManager.Current,
                            @event,
                            cancel).ConfigureAwait(false);
                        committedEvents.Add(committedEvent);
                        eventLogSequenceNumber++;
                        aggregateRootVersion++;
                    }

                    await _aggregateRoots.IncrementVersionFor(
                        transaction,
                        events.EventSource,
                        events.AggregateRoot.Id,
                        events.ExpectedAggregateRootVersion,
                        aggregateRootVersion,
                        cancel).ConfigureAwait(false);

                    return new CommittedAggregateEvents(events.EventSource, events.AggregateRoot.Id, committedEvents);
                },
                           cancellationToken : cancellationToken).ConfigureAwait(false));
            }
            catch (MongoWaitQueueFullException ex)
            {
                throw new EventStoreUnavailable("Mongo wait queue is full", ex);
            }
        }
Пример #3
0
    /// <inheritdoc />
    protected override void Setup(IServiceProvider services)
    {
        _eventStore       = services.GetRequiredService <IEventStore>();
        _executionContext = Runtime.CreateExecutionContextFor(ConfiguredTenants.First());

        var events = new List <UncommittedEvent>();

        for (var n = 0; n < EventsToCommit; n++)
        {
            events.Add(new UncommittedEvent(
                           new EventSourceId("46c4de33-9a60-4465-97ab-a2a7f5b7e6a3"),
                           new Artifact(new ArtifactId(Guid.Parse("08db4b0a-3724-444f-9968-ada44922fb78")), ArtifactGeneration.First),
                           false,
                           "{ \"hello\": \"world\" }"));
        }

        _eventsToCommit = new UncommittedAggregateEvents(
            new EventSourceId("46c4de33-9a60-4465-97ab-a2a7f5b7e6a3"),
            new Artifact(new ArtifactId(Guid.Parse("1ad7a5dc-12e9-493a-ba10-714c88be4da7")), ArtifactGeneration.First),
            AggregateRootVersion.Initial,
            events);
    }
    Try <bool> TryCheckIfDesiredState(
        EmbeddingCurrentState current,
        IEnumerable <UncommittedEvents> allTransitionEvents,
        Func <EmbeddingCurrentState, Try <bool> > isDesiredState,
        out UncommittedAggregateEvents eventsToCommit)
    {
        eventsToCommit = null;
        var isDesired = isDesiredState(current);

        if (!isDesired.Success)
        {
            return(isDesired.Exception);
        }
        if (!isDesired.Result)
        {
            return(false);
        }
        var flattenedTransitionEvents = from uncommittedEvents in allTransitionEvents
                                        from @event in uncommittedEvents
                                        select @event;

        eventsToCommit = CreateUncommittedAggregateEvents(new UncommittedEvents(flattenedTransitionEvents.ToArray()), current);
        return(true);
    }