Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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();
            }
        }
Exemplo n.º 3
0
        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();
            }
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public async Task SaveEventAsync <TAggregate>(IDomainEvent @event, int?expectedVersion) where TAggregate : IAggregateRoot
        {
            var currentVersion = await _eventRepository.GetCountAsync(d => d.AggregateId == @event.AggregateRootId);

            var nextVersion = _versionService.GetNextVersion(@event.AggregateRootId, currentVersion, expectedVersion);

            var eventDocument = _eventDocumentFactory.CreateEvent(@event, nextVersion);

            await _eventRepository.CreateDocumentAsync(eventDocument);
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public async Task SaveEventAsync <TAggregate>(IDomainEvent @event, int?expectedVersion) where TAggregate : IAggregateRoot
        {
            var eventFilter = Builders <EventDocument> .Filter.Eq("aggregateId", @event.AggregateRootId.ToString());

            var currentVersion = await _dbContext.Events.Find(eventFilter).CountDocumentsAsync();

            var nextVersion = _versionService.GetNextVersion(@event.AggregateRootId, (int)currentVersion, expectedVersion);

            var eventDocument = _eventDocumentFactory.CreateEvent(@event, nextVersion);

            await _dbContext.Events.InsertOneAsync(eventDocument);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }