コード例 #1
0
        public async Task SaveAsync(IEntityId aggregateId, int originalVersion, IReadOnlyCollection <IDomainEvent> events, string name = "Aggregate name")
        {
            if (events.Count == 0)
            {
                return;
            }

            var query = $@"INSERT INTO {EventStoreTableName} ({EventStoreListOfColumnsInsert})
                           VALUES (@Id, @CreatedAt, @Version, @Name, @AggregateId, @Data, @Aggregate)";

            var listOfEvents = events.Select(ev => new
            {
                Id        = Guid.NewGuid(),
                Data      = JsonConvert.SerializeObject(ev, Formatting.Indented, _serializerService._jsonSerializerSettings),
                Aggregate = name,
                ev.CreatedAt,
                ev.GetType().Name,
                AggregateId = aggregateId.ToString(),
                Version     = ++originalVersion
            });

            using (var connection = _connectionFactory.SqlConnection())
            {
                await connection.ExecuteAsync(query, listOfEvents);
            }
        }
コード例 #2
0
        public async Task SaveEventsAsync(IEntityId aggregateId, int expectedVersion, IEnumerable <IDomainEvent> events)
        {
            if (events.Count() == 0)
            {
                return;
            }

            var query =
                $@"INSERT INTO {EventStoreTableName} ({EventStoreListOfColumnsInsert})
                    VALUES (@Id,@CreatedAt,@Version,@Name,@AggregateId,@Data);";

            var listOfEvents = events.Select(ev => new
            {
                ev.CreatedAt,
                Data = JsonConvert.SerializeObject(ev, Formatting.Indented, _jsonSerializerSettings),
                Id   = Guid.NewGuid(),
                ev.GetType().Name,
                AggregateId = aggregateId.ToString(),
                Version     = ++expectedVersion
            });

            using (var connection = _connectionFactory.SqlConnection())
            {
                await connection.ExecuteAsync(query, listOfEvents);
            }
        }
コード例 #3
0
        public async Task SaveEventsAsync(IEntityId aggregateId, int version, IReadOnlyCollection <IDomainEvent> events)
        {
            var stream = $"{aggregateId.GetType().Name}-{aggregateId.ToString()}";

            await Connection.AppendToStreamAsync(stream, version, events.Select(e =>
            {
                var json = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(e, SerializerSettings));
                return(new EventData(Guid.NewGuid(), e.GetType().Name, true, json, new byte[] { }));
            }));
        }
コード例 #4
0
        public async Task <IReadOnlyCollection <IDomainEvent> > LoadEventsAsync(IEntityId aggregateId)
        {
            var stream = $"{aggregateId.GetType().Name}-{aggregateId.ToString()}";

            var streamEvents = await Connection.ReadStreamEventsForwardAsync(stream, 0, 4096, false);

            return(streamEvents.Events
                   .Select(DeserializeDomainEvent)
                   .ToList()
                   .AsReadOnly());
        }
コード例 #5
0
        public async Task SaveAsync(IEntityId aggregateId, int originatingVersion, IReadOnlyCollection <IDomainEvent> events, string aggregateName, CancellationToken cancellationToken)
        {
            if (!events.Any())
            {
                return;
            }

            var records = events.Select(evt => EventStoreRecord.CreateRecord(
                                            recordId: Guid.NewGuid(),
                                            serializedData: JsonConvert.SerializeObject(evt, Formatting.Indented, _jsonSettings),
                                            version: ++originatingVersion,
                                            createdAt: evt.CreatedAt,
                                            domainEventName: evt.GetType().Name,
                                            aggregateName: aggregateName,
                                            aggregateRootId: aggregateId.ToString()
                                            ));

            await _dbContext.EventStoreRecords.AddRangeAsync(records, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);
        }
コード例 #6
0
        public async Task <IReadOnlyCollection <IDomainEvent> > LoadAsync(IEntityId aggregateId)
        {
            if (aggregateId == null)
            {
                throw new Exception("Not found");
            }

            var query = new StringBuilder($@"SELECT {EventStoreListOfColumnsSelect} FROM {EventStoreTableName}");

            query.Append(" WHERE [AggregateId] = @AggregateId ");
            query.Append(" ORDER BY [Version] ASC;");

            using (var connection = _connectionFactory.SqlConnection())
            {
                var events       = (await connection.QueryAsync <EventStoreDao>(query.ToString(), new { AggregateId = aggregateId.ToString() }));
                var domainEvents = events.Select(_serializerService.TransformEvent).Where(@event => @event != null).ToList().AsReadOnly();
                return(domainEvents);
            }
        }
コード例 #7
0
        public async Task <IReadOnlyCollection <IDomainEvent> > LoadAsync(IEntityId aggregateRootId, CancellationToken cancellationToken)
        {
            if (aggregateRootId == null)
            {
                throw new AggregateException($"{nameof(aggregateRootId)} cannot be null");
            }

            var events = await _dbContext.EventStoreRecords.Where(record => record.AggregateRootId == aggregateRootId.ToString())
                         .OrderBy(record => record.Version).ToListAsync(cancellationToken).ConfigureAwait(false);

            return(events.Select(Transform).ToList().AsReadOnly());
        }
コード例 #8
0
        public async Task <IReadOnlyCollection <IDomainEvent> > LoadAsync(IEntityId aggregateRootId)
        {
            if (aggregateRootId == null)
            {
                throw new AggregateRootNotProvidedException("AggregateRootId cannot be null");
            }

            var query = new StringBuilder($@"SELECT {EventStoreListOfColumnsSelect} FROM {EventStoreTableName}");

            query.Append(" WHERE [AggregateId] = @AggregateId ");
            query.Append(" ORDER BY [Version] ASC;");

            using (var connection = _connectionFactory.SqlConnection())
            {
                var events       = (await connection.QueryAsync <EventStoreDao>(query.ToString(), aggregateRootId != null ? new { AggregateId = aggregateRootId.ToString() } : null)).ToList();
                var domainEvents = events.Select(TransformEvent).Where(x => x != null).ToList().AsReadOnly();

                return(domainEvents);
            }
        }