Example #1
0
        public void Create_sets_CorrelationId_correctly()
        {
            Type sourceType    = new Fixture().Create <Type>();
            var  sourceId      = Guid.NewGuid();
            var  correlationId = Guid.NewGuid();

            var actual = Correlation.Create(sourceType, sourceId, correlationId);

            actual.CorrelationId.Should().Be(correlationId);
        }
Example #2
0
        public void Create_returns_Correlation_instance()
        {
            Type sourceType    = new Fixture().Create <Type>();
            var  sourceId      = Guid.NewGuid();
            var  correlationId = Guid.NewGuid();

            var actual = Correlation.Create(sourceType, sourceId, correlationId);

            actual.Should().NotBeNull();
        }
Example #3
0
        public void Create_sets_PartitionKey_correctly()
        {
            Type sourceType    = new Fixture().Create <Type>();
            var  sourceId      = Guid.NewGuid();
            var  correlationId = Guid.NewGuid();

            var actual = Correlation.Create(sourceType, sourceId, correlationId);

            actual.PartitionKey.Should().Be(AggregateEntity.GetPartitionKey(sourceType, sourceId));
        }
        private async Task Save <T>(
            List <IDomainEvent> domainEvents,
            string operationId,
            Guid?correlationId,
            string contributor,
            CancellationToken cancellationToken)
            where T : class, IEventSourced
        {
            var envelopes = new List <Envelope <IDomainEvent> >(
                from domainEvent in domainEvents
                let messageId = Guid.NewGuid()
                                select new Envelope <IDomainEvent>(messageId, domainEvent, operationId, correlationId, contributor));

            var batch = new TableBatchOperation();

            foreach (Envelope <IDomainEvent> envelope in envelopes)
            {
                batch.Insert(PersistentEvent.Create(typeof(T), envelope, _serializer));
                batch.Insert(PendingEvent.Create(typeof(T), envelope, _serializer));
            }

            Guid sourceId = domainEvents.First().SourceId;

            if (correlationId.HasValue)
            {
                batch.Insert(Correlation.Create(typeof(T), sourceId, correlationId.Value));
            }

            try
            {
                await _eventTable.ExecuteBatch(batch, cancellationToken).ConfigureAwait(false);
            }
            catch (StorageException exception) when(correlationId.HasValue)
            {
                string filter = Correlation.GetFilter(typeof(T), sourceId, correlationId.Value);
                var    query  = new TableQuery <Correlation> {
                    FilterString = filter
                };

                if (await _eventTable.Any(query, cancellationToken).ConfigureAwait(false))
                {
                    throw new DuplicateCorrelationException(
                              typeof(T),
                              sourceId,
                              correlationId.Value,
                              exception);
                }

                throw;
            }
        }
        public async Task SaveEvents_inserts_correlation_entity_correctly()
        {
            // Arrange
            var fixture = new Fixture();
            var user    = new FakeUser(Guid.NewGuid(), fixture.Create <string>());
            IList <IDomainEvent> domainEvents = user.FlushPendingEvents().ToList();
            string operationId   = fixture.Create <string>();
            var    correlationId = Guid.NewGuid();
            string contributor   = fixture.Create <string>();

            // Act
            await _sut.SaveEvents <FakeUser>(domainEvents, operationId, correlationId, contributor);

            // Assert
            string filter = Correlation.GetFilter(typeof(FakeUser), user.Id, correlationId);
            var    query  = new TableQuery <Correlation> {
                FilterString = filter
            };
            IEnumerable <Correlation> actual = await s_eventTable.ExecuteQuerySegmentedAsync(query, default);

            actual.Should().ContainSingle().Which.ShouldBeEquivalentTo(
                new { CorrelationId = correlationId },
                opts => opts.ExcludingMissingMembers());
        }