示例#1
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));
        }
示例#2
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);
        }
示例#3
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();
        }
        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;
            }
        }