public async Task SaveAsync <TAggregate>(IAggregateRoot <TAggregate> aggregateRoot, string stream) { await _connection.AppendToStreamAsync(stream, aggregateRoot.OriginalVersion, aggregateRoot.GetUncommittedEvents()); aggregateRoot.ClearUncommittedEvents(); }
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; } }
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); }
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()); }
static void PublishEvent(IAggregateRoot root, IServiceBus bus) { bus.PublishAsync(root.GetUncommittedEvents()); root.AcceptUncommittedEvents(); }