public void Save(Type aggregateType, Guid aggregateRootId, IDomainCommand command, IEnumerable <IDomainEvent> events) { using (var dbContext = _dbContextFactory.CreateDbContext()) { var aggregateEntity = dbContext.Aggregates.FirstOrDefault(x => x.Id == aggregateRootId); if (aggregateEntity == null) { var newAggregateEntity = _aggregateEntityFactory.CreateAggregate(aggregateType, aggregateRootId); dbContext.Aggregates.Add(newAggregateEntity); } if (command != null) { var newCommandEntity = _commandEntityFactory.CreateCommand(command); dbContext.Commands.Add(newCommandEntity); } foreach (var @event in events) { var currentVersion = dbContext.Events.Count(x => x.AggregateId == @event.AggregateRootId); var nextVersion = _versionService.GetNextVersion(@event.AggregateRootId, currentVersion, command?.ExpectedVersion); var newEventEntity = _eventEntityFactory.CreateEvent(@event, nextVersion); dbContext.Events.Add(newEventEntity); } dbContext.SaveChanges(); } }
public void Save(SaveStoreData request) { using (var dbContext = _dbContextFactory.CreateDbContext()) { var aggregateEntity = dbContext.Aggregates.FirstOrDefault(x => x.Id == request.AggregateRootId); if (aggregateEntity == null) { var newAggregateEntity = _aggregateEntityFactory.CreateAggregate(request.AggregateType, request.AggregateRootId); dbContext.Aggregates.Add(newAggregateEntity); } if (request.DomainCommand != null) { var newCommandEntity = _commandEntityFactory.CreateCommand(request.DomainCommand); dbContext.Commands.Add(newCommandEntity); } foreach (var @event in request.Events) { var currentVersion = dbContext.Events.Count(x => x.AggregateId == @event.AggregateRootId); var nextVersion = _versionService.GetNextVersion(@event.AggregateRootId, currentVersion, request.DomainCommand?.ExpectedVersion); var newEventEntity = _eventEntityFactory.CreateEvent(@event, nextVersion); dbContext.Events.Add(newEventEntity); } dbContext.SaveChanges(); } }
/// <inheritdoc /> public async Task SaveAggregateAsync <TAggregate>(Guid id) where TAggregate : IAggregateRoot { using (var dbContext = _dbContextFactory.CreateDbContext()) { var aggregateEntity = await dbContext.Aggregates.FirstOrDefaultAsync(x => x.Id == id); if (aggregateEntity == null) { var newAggregateEntity = _aggregateEntityFactory.CreateAggregate <TAggregate>(id); await dbContext.Aggregates.AddAsync(newAggregateEntity); await dbContext.SaveChangesAsync(); } } }
/// <inheritdoc /> public async Task SaveCommandAsync <TAggregate>(IDomainCommand command) where TAggregate : IAggregateRoot { using (var dbContext = _dbContextFactory.CreateDbContext()) { var aggregateEntity = await dbContext.Aggregates.FirstOrDefaultAsync(x => x.Id == command.AggregateRootId); if (aggregateEntity == null) { var newAggregateEntity = _aggregateEntityFactory.CreateAggregate <TAggregate>(command.AggregateRootId); await dbContext.Aggregates.AddAsync(newAggregateEntity); } var newCommandEntity = _commandEntityFactory.CreateCommand(command); await dbContext.Commands.AddAsync(newCommandEntity); await dbContext.SaveChangesAsync(); } }
/// <inheritdoc /> public async Task SaveEventAsync <TAggregate>(IDomainEvent @event) where TAggregate : IAggregateRoot { using (var dbContext = _dbContextFactory.CreateDbContext()) { var aggregateEntity = await dbContext.Aggregates.FirstOrDefaultAsync(x => x.Id == @event.AggregateRootId); if (aggregateEntity == null) { var newAggregateEntity = _aggregateEntityFactory.CreateAggregate <TAggregate>(@event.AggregateRootId); dbContext.Aggregates.Add(newAggregateEntity); } var currentSequenceCount = await dbContext.Events.CountAsync(x => x.AggregateId == @event.AggregateRootId); var newEventEntity = _eventEntityFactory.CreateEvent(@event, currentSequenceCount + 1); dbContext.Events.Add(newEventEntity); await dbContext.SaveChangesAsync(); } }
/// <inheritdoc /> public async Task SaveEventAsync <TAggregate>(IDomainEvent @event, int?expectedVersion) where TAggregate : IAggregateRoot { using (var dbContext = _dbContextFactory.CreateDbContext()) { var aggregateEntity = await dbContext.Aggregates.FirstOrDefaultAsync(x => x.Id == @event.AggregateRootId); if (aggregateEntity == null) { var newAggregateEntity = _aggregateEntityFactory.CreateAggregate <TAggregate>(@event.AggregateRootId); await dbContext.Aggregates.AddAsync(newAggregateEntity); } var currentVersion = await dbContext.Events.CountAsync(x => x.AggregateId == @event.AggregateRootId); var nextVersion = _versionService.GetNextVersion(@event.AggregateRootId, currentVersion, expectedVersion); var newEventEntity = _eventEntityFactory.CreateEvent(@event, nextVersion); await dbContext.Events.AddAsync(newEventEntity); await dbContext.SaveChangesAsync(); } }