Exemple #1
0
        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();
            }
        }
Exemple #2
0
        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();
            }
        }
Exemple #3
0
        /// <inheritdoc />
        public async Task SaveEventAsync <TAggregate>(IDomainEvent @event, int?expectedVersion) where TAggregate : IAggregateRoot
        {
            using (var dbContext = _dbContextFactory.CreateDbContext())
            {
                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();
            }
        }
Exemple #4
0
        /// <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();
            }
        }