public void Save(SaveStoreData request) { var aggregateFilter = Builders <AggregateDocument> .Filter.Eq("_id", request.AggregateRootId.ToString()); var aggregateDocument = _dbContext.Aggregates.Find(aggregateFilter).FirstOrDefault(); if (aggregateDocument == null) { var newAggregateDocument = _aggregateDocumentFactory.CreateAggregate(request.AggregateType, request.AggregateRootId); _dbContext.Aggregates.InsertOne(newAggregateDocument); } if (request.DomainCommand != null) { var commandDocument = _commandDocumentFactory.CreateCommand(request.DomainCommand); _dbContext.Commands.InsertOne(commandDocument); } foreach (var @event in request.Events) { var eventFilter = Builders <EventDocument> .Filter.Eq("aggregateId", @event.AggregateRootId.ToString()); var currentVersion = _dbContext.Events.Find(eventFilter).CountDocuments(); var nextVersion = _versionService.GetNextVersion(@event.AggregateRootId, (int)currentVersion, request.DomainCommand?.ExpectedVersion); var eventDocument = _eventDocumentFactory.CreateEvent(@event, nextVersion); _dbContext.Events.InsertOne(eventDocument); } }
public void Save(SaveStoreData request) { var aggregateDocument = _aggregateRepository.GetDocumentAsync(request.AggregateRootId.ToString()).GetAwaiter().GetResult(); if (aggregateDocument == null) { var newAggregateDocument = _aggregateDocumentFactory.CreateAggregate(request.AggregateType, request.AggregateRootId); _aggregateRepository.CreateDocumentAsync(newAggregateDocument).GetAwaiter().GetResult(); } if (request.DomainCommand != null) { var commandDocument = _commandDocumentFactory.CreateCommand(request.DomainCommand); _commandRepository.CreateDocumentAsync(commandDocument).GetAwaiter().GetResult(); } foreach (var @event in request.Events) { var currentVersion = _eventRepository.GetCountAsync(d => d.AggregateId == @event.AggregateRootId).GetAwaiter().GetResult(); var nextVersion = _versionService.GetNextVersion(@event.AggregateRootId, currentVersion, request.DomainCommand?.ExpectedVersion); var eventDocument = _eventDocumentFactory.CreateEvent(@event, nextVersion); _eventRepository.CreateDocumentAsync(eventDocument).GetAwaiter().GetResult(); } }
public void Save(Type aggregateType, Guid aggregateRootId, IDomainCommand command, IEnumerable <IDomainEvent> events) { var aggregateDocument = _aggregateRepository.GetDocumentAsync(aggregateRootId.ToString()).GetAwaiter().GetResult(); if (aggregateDocument == null) { var newAggregateDocument = _aggregateDocumentFactory.CreateAggregate(aggregateType, aggregateRootId); _aggregateRepository.CreateDocumentAsync(newAggregateDocument).GetAwaiter().GetResult(); } if (command != null) { var commandDocument = _commandDocumentFactory.CreateCommand(command); _commandRepository.CreateDocumentAsync(commandDocument).GetAwaiter().GetResult(); } foreach (var @event in events) { var currentVersion = _eventRepository.GetCountAsync(d => d.AggregateId == @event.AggregateRootId).GetAwaiter().GetResult(); var nextVersion = _versionService.GetNextVersion(@event.AggregateRootId, currentVersion, command?.ExpectedVersion); var eventDocument = _eventDocumentFactory.CreateEvent(@event, nextVersion); _eventRepository.CreateDocumentAsync(eventDocument).GetAwaiter().GetResult(); } }
/// <inheritdoc /> public async Task SaveAggregateAsync <TAggregate>(Guid id) where TAggregate : IAggregateRoot { var aggregateDocument = await _aggregateRepository.GetDocumentAsync(id.ToString()); if (aggregateDocument == null) { var newAggregateDocument = _aggregateDocumentFactory.CreateAggregate <TAggregate>(id); await _aggregateRepository.CreateDocumentAsync(newAggregateDocument); } }
/// <inheritdoc /> public async Task SaveAggregateAsync <TAggregate>(Guid id) where TAggregate : IAggregateRoot { var aggregateFilter = Builders <AggregateDocument> .Filter.Eq("_id", id.ToString()); var aggregateDocument = await _dbContext.Aggregates.Find(aggregateFilter).FirstOrDefaultAsync(); if (aggregateDocument == null) { var newAggregateDocument = _aggregateDocumentFactory.CreateAggregate <TAggregate>(id); await _dbContext.Aggregates.InsertOneAsync(newAggregateDocument); } }
public async Task SaveCommandAsync <TAggregate>(IDomainCommand command) where TAggregate : IAggregateRoot { var aggregateDocument = await _aggregateRepository.GetDocumentAsync(command.AggregateRootId.ToString()); if (aggregateDocument == null) { var newAggregateDocument = _aggregateDocumentFactory.CreateAggregate <TAggregate>(command.AggregateRootId); await _aggregateRepository.CreateDocumentAsync(newAggregateDocument); } var commandDocument = _commandDocumentFactory.CreateCommand(command); await _commandRepository.CreateDocumentAsync(commandDocument); }
public async Task SaveEventAsync <TAggregate>(IDomainEvent @event) where TAggregate : IAggregateRoot { var aggregateDocument = await _aggregateRepository.GetDocumentAsync(@event.AggregateRootId.ToString()); if (aggregateDocument == null) { var newAggregateDocument = _aggregateDocumentFactory.CreateAggregate <TAggregate>(@event); await _aggregateRepository.CreateDocumentAsync(newAggregateDocument); } var currentVersion = await _eventRepository.GetCountAsync(d => d.AggregateId == @event.AggregateRootId); var eventDocument = _eventDocumentFactory.CreateEvent(@event, currentVersion + 1); await _eventRepository.CreateDocumentAsync(eventDocument); }
public async Task SaveCommandAsync <TAggregate>(IDomainCommand command) where TAggregate : IAggregateRoot { var aggregateFilter = Builders <AggregateDocument> .Filter.Eq("_id", command.AggregateRootId.ToString()); var aggregate = await _dbContext.Aggregates.Find(aggregateFilter).FirstOrDefaultAsync(); if (aggregate == null) { var aggregateDocument = _aggregateDocumentFactory.CreateAggregate <TAggregate>(command.AggregateRootId); await _dbContext.Aggregates.InsertOneAsync(aggregateDocument); } var commandDocument = _commandDocumentFactory.CreateCommand(command); await _dbContext.Commands.InsertOneAsync(commandDocument); }
public async Task SaveEventAsync <TAggregate>(IDomainEvent @event) where TAggregate : IAggregateRoot { var aggregateFilter = Builders <AggregateDocument> .Filter.Eq("_id", @event.AggregateRootId.ToString()); var aggregate = await _dbContext.Aggregates.Find(aggregateFilter).FirstOrDefaultAsync(); if (aggregate == null) { var aggregateDocument = _aggregateDocumentFactory.CreateAggregate <TAggregate>(@event.AggregateRootId); await _dbContext.Aggregates.InsertOneAsync(aggregateDocument); } var eventFilter = Builders <EventDocument> .Filter.Eq("aggregateId", @event.AggregateRootId.ToString()); var currentVersion = await _dbContext.Events.Find(eventFilter).CountAsync(); var eventDocument = _eventDocumentFactory.CreateEvent(@event, currentVersion + 1); await _dbContext.Events.InsertOneAsync(eventDocument); }