Exemplo n.º 1
0
        public async Task SaveAsync <TAggregate>(IAggregateRoot <TAggregate> aggregateRoot, string stream)
        {
            await _connection.AppendToStreamAsync(stream, aggregateRoot.OriginalVersion,
                                                  aggregateRoot.GetUncommittedEvents());

            aggregateRoot.ClearUncommittedEvents();
        }
Exemplo n.º 2
0
        public void PersistEvents(IAggregateRoot aggregate)
        {
            try
            {
                var events = aggregate.GetUncommittedEvents();

                if (!events.Any())
                {
                    return;
                }

                Database.Open().Events.Insert(
                    Id: Guid.NewGuid(),
                    AggregateId: aggregate.Id,
                    Version: aggregate.Version + 1,
                    Events: _serializer.Serialize(events),
                    Created: DateTime.UtcNow
                    );
            }
            catch (SqlException ex)
            {
                if (ex.Number == UniqueKeyViolation)
                {
                    throw new ConcurrencyException();
                }

                throw;
            }
        }
Exemplo n.º 3
0
        private EventStream CreateEventStream(IAggregateRoot aggregateRoot, ICommand command)
        {
            var uncommittedEvents = aggregateRoot.GetUncommittedEvents().ToList();
            aggregateRoot.ClearUncommittedEvents();

            var aggregateRootTypeCode = _aggregateRootTypeProvider.GetTypeCode(aggregateRoot.GetType());

            foreach (var evnt in uncommittedEvents)
            {
                evnt.Version = aggregateRoot.Version + 1;
            }
            return new EventStream(
                command.Id,
                aggregateRoot.UniqueId,
                aggregateRootTypeCode,
                aggregateRoot.Version + 1,
                DateTime.Now,
                uncommittedEvents);
        }
Exemplo n.º 4
0
            public async Task AppendUncommittedEventsToNewStream([Frozen] IEventStoreConnection connection,
                                                                 IAggregateRoot <TestAggregate> aggregateRoot, EventStoreRepository sut)
            {
                await sut.SaveAsync(aggregateRoot);

                await connection.Received(1).AppendToStreamAsync($"{aggregateRoot.GetType().Name}-{aggregateRoot.Id}",
                                                                 aggregateRoot.OriginalVersion, aggregateRoot.GetUncommittedEvents());
            }
Exemplo n.º 5
0
 static void PublishEvent(IAggregateRoot root, IServiceBus bus)
 {
     bus.PublishAsync(root.GetUncommittedEvents());
     root.AcceptUncommittedEvents();
 }