예제 #1
0
        private async Task <IEnumerable <IDomainEvent> > Load <T>(
            Guid sourceId,
            int afterVersion,
            CancellationToken cancellationToken)
            where T : class, IEventSourced
        {
            string filter = CombineFilters(
                GenerateFilterCondition(
                    nameof(ITableEntity.PartitionKey),
                    Equal,
                    EventTableEntity.GetPartitionKey(typeof(T), sourceId)),
                And,
                GenerateFilterCondition(
                    nameof(ITableEntity.RowKey),
                    GreaterThan,
                    EventTableEntity.GetRowKey(afterVersion)));

            var query = new TableQuery <EventTableEntity>().Where(filter);

            IEnumerable <EventTableEntity> events = await _eventTable
                                                    .ExecuteQuery(query, cancellationToken)
                                                    .ConfigureAwait(false);

            return(new List <IDomainEvent>(events
                                           .Select(e => e.EventJson)
                                           .Select(_serializer.Deserialize)
                                           .Cast <IDomainEvent>()));
        }
예제 #2
0
        public async Task SaveEvents_inserts_event_entities_correctly()
        {
            // Arrange
            var created         = fixture.Create <FakeUserCreated>();
            var usernameChanged = fixture.Create <FakeUsernameChanged>();
            var events          = new DomainEvent[] { created, usernameChanged };
            var correlationId   = Guid.NewGuid();

            RaiseEvents(userId, events);

            // Act
            await sut.SaveEvents <FakeUser>(events, correlationId);

            // Assert
            string partitionKey = EventTableEntity.GetPartitionKey(typeof(FakeUser), userId);
            var    query        = new TableQuery <EventTableEntity>().Where($"PartitionKey eq '{partitionKey}'");
            IEnumerable <EventTableEntity> persistentEvents = s_eventTable.ExecuteQuery(query);

            foreach (var t in persistentEvents.Zip(events, (persistent, source)
                                                   => new { Persistent = persistent, Source = source }))
            {
                var actual = new
                {
                    t.Persistent.RowKey,
                    t.Persistent.Version,
                    t.Persistent.EventType,
                    t.Persistent.CorrelationId,
                    Event = (DomainEvent)serializer.Deserialize(t.Persistent.EventJson),
                    t.Persistent.RaisedAt
                };
                actual.ShouldBeEquivalentTo(new
                {
                    RowKey = EventTableEntity.GetRowKey(t.Source.Version),
                    t.Source.Version,
                    EventType     = t.Source.GetType().FullName,
                    CorrelationId = correlationId,
                    Event         = t.Source,
                    t.Source.RaisedAt
                });
            }
        }
예제 #3
0
        private async Task <List <EventTableEntity> > GetPersistentEvents(
            string persistentPartition,
            int version,
            CancellationToken cancellationToken)
        {
            var query = new TableQuery <EventTableEntity>();

            string filter = CombineFilters(
                GenerateFilterCondition(
                    nameof(ITableEntity.PartitionKey),
                    Equal,
                    persistentPartition),
                And,
                GenerateFilterCondition(
                    nameof(ITableEntity.RowKey),
                    GreaterThanOrEqual,
                    EventTableEntity.GetRowKey(version)));

            return(new List <EventTableEntity>(await _eventTable
                                               .ExecuteQuery(query.Where(filter), cancellationToken)
                                               .ConfigureAwait(false)));
        }