Beispiel #1
0
 private static Task PerformCoordinatedReconcileAsync(
     EventCentricAggregateRoot aggregate,
     IAggregateReconciliationProxy proxy,
     CancellationToken?cancellationToken)
 {
     return(proxy.OverwriteAsync(aggregate, cancellationToken: cancellationToken));
 }
        public virtual Task SaveAsync(
            EventCentricAggregateRoot aggregate,
            CancellationToken?cancellationToken = default)
        {
            PerformSave(aggregate);

            return(Task.CompletedTask);
        }
        protected override void PerformSave(EventCentricAggregateRoot aggregate)
        {
            if (save is null)
            {
                throw new NotImplementedException();
            }

            save(aggregate);
        }
Beispiel #4
0
 private Task ReconcileAsync(
     EventCentricAggregateRoot aggregate,
     IAggregateReconciliationProxy proxy,
     CancellationToken?cancellationToken)
 {
     return(aggregate.CoordinateAsync(
                () => PerformCoordinatedReconcileAsync(aggregate, proxy, cancellationToken),
                cancellationToken: cancellationToken,
                timeout: timeout));
 }
        public async Task GivenEventsThenAnAggregateHydratedWithThoseEventsIsReturnedAsync()
        {
            var expected = new SerializableEventCentricAggregateRoot();
            var context  = new SerializableMessage();

            expected.Set(new SetRequest(context, Guid.NewGuid()));
            expected.Set(new SetRequest(context, Guid.NewGuid()));

            IEnumerable <DomainEvent> events = expected.GetUncommittedChanges();

            expected.MarkChangesAsCommitted();

            var reconciler = new TestableDefaultAggregateFactory();
            EventCentricAggregateRoot actual = await reconciler.CreateAsync(events);

            Assert.NotSame(expected, actual);
            Assert.Equal(expected, actual);
        }
Beispiel #6
0
        protected virtual async Task ApplyAsync(
            EventCentricAggregateRoot aggregate,
            IEnumerable <DomainEvent> events,
            IAggregateReconciliationProxy proxy,
            Reference reference,
            CancellationToken?cancellationToken = default)
        {
            if (events.Any())
            {
                try
                {
                    aggregate.LoadFromHistory(events);

                    await proxy
                    .SaveAsync(aggregate, cancellationToken : cancellationToken)
                    .ConfigureAwait(false);

                    await OnAggregateReconciledAsync(reference, events, cancellationToken : cancellationToken)
                    .ConfigureAwait(false);
                }
                catch (AggregateHistoryInvalidForStateException invalid)
                {
                    await
                    OnAggregateConflictDetectedAsync(
                        invalid.Aggregate,
                        events,
                        invalid.StartingVersion,
                        invalid.Aggregate.Version,
                        cancellationToken : cancellationToken)
                    .ConfigureAwait(false);
                }
                catch (AggregateConflictDetectedException conflict)
                {
                    await
                    OnAggregateConflictDetectedAsync(
                        reference,
                        events,
                        conflict.Received,
                        conflict.Persisted,
                        cancellationToken : cancellationToken)
                    .ConfigureAwait(false);
                }
            }
        }
Beispiel #7
0
        public async Task <EventCentricAggregateRoot> CreateAsync(
            IEnumerable <DomainEvent> events,
            CancellationToken?cancellationToken = default)
        {
            if (!events.SafeAny())
            {
                throw new DomainEventsMissingException();
            }

            Reference aggregate = events.First().Aggregate;

            EventCentricAggregateRoot instance = await
                                                 CreateAsync(
                aggregate,
                cancellationToken : cancellationToken)
                                                 .ConfigureAwait(false);

            instance.LoadFromHistory(events);

            return(instance);
        }
 protected abstract void PerformSave(EventCentricAggregateRoot aggregate);
 protected abstract void PerformOverwrite(EventCentricAggregateRoot aggregate);
Beispiel #10
0
 public virtual Task ReconcileAsync(
     EventCentricAggregateRoot aggregate,
     CancellationToken?cancellationToken = default)
 {
     return(ReconcileAsync(new[] { aggregate }, cancellationToken: cancellationToken));
 }
        public async Task GivenAStreamThenTheSnapshotContainsTheAggregatesThatComprizeThatStreamAsync()
        {
            SerializableEventCentricAggregateRoot expected = CreateAggregate(out IEnumerable <DomainEvent> events);
            var other = new SerializableEventCentricAggregateRoot(expected.Id);

            _ = factory
                .Setup(factory => factory.CreateAsync(
                           It.IsAny <Reference>(),
                           It.IsAny <CancellationToken?>()))
                .ReturnsAsync(other);

            _ = proxy
                .Setup(proxy => proxy.GetAsync(
                           It.IsAny <Reference>(),
                           It.IsAny <CancellationToken?>()))
                .ReturnsAsync(default(EventCentricAggregateRoot) !);

            _ = proxy
                .Setup(proxy => proxy.GetAllAsync(It.IsAny <CancellationToken?>()))
                .ReturnsAsync(new[] { other });

            _ = store
                .Setup(store => store.ReadAsync(
                           It.Is <ulong>(lastIndex => lastIndex == ulong.MinValue),
                           It.IsAny <CancellationToken?>(),
                           It.IsAny <ushort>()))
                .ReturnsAsync(new[] { new SequencedEvents(1, events.ToArray()) });

            _ = store
                .Setup(store => store.ReadAsync(
                           It.Is <ulong>(lastIndex => lastIndex > ulong.MinValue),
                           It.IsAny <CancellationToken?>(),
                           It.IsAny <ushort>()))
                .ReturnsAsync(Enumerable.Empty <SequencedEvents>());

            var instance = new DefaultSnapshotProvider <SequencedEvents>(
                store.Object,
                factory.Object,
                () => type => proxy.Object);

            ISnapshot?snapshot = await instance.GenerateAsync();

            Assert.NotNull(snapshot);

            EventCentricAggregateRoot actual = Assert.Single(snapshot !.Aggregates);

            Assert.Equal(expected, actual);

            factory.Verify(
                factory => factory.CreateAsync(
                    It.IsAny <Reference>(),
                    It.IsAny <CancellationToken?>()),
                times: Times.Once);

            factory.Verify(
                factory => factory.CreateAsync(
                    It.Is <Reference>(reference => reference.Id == expected.Id),
                    It.IsAny <CancellationToken?>()),
                times: Times.Once);

            proxy.Verify(
                proxy => proxy.GetAsync(
                    It.IsAny <Reference>(),
                    It.IsAny <CancellationToken?>()),
                times: Times.Once);

            proxy.Verify(
                proxy => proxy.GetAllAsync(It.IsAny <CancellationToken?>()),
                times: Times.Once);
        }