public async Task SetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            SerializedSnapshot serializedSnapshot,
            CancellationToken cancellationToken)
        {
            var entity = new SnapshotEntity
            {
                AggregateId             = identity.Value,
                AggregateName           = aggregateType.GetAggregateName().Value,
                AggregateSequenceNumber = serializedSnapshot.Metadata.AggregateSequenceNumber,
                Metadata = serializedSnapshot.SerializedMetadata,
                Data     = serializedSnapshot.SerializedData
            };

            using (var dbContext = _contextProvider.CreateContext())
            {
                dbContext.Add(entity);

                try
                {
                    await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                }
                catch (DbUpdateException ex) when(ex.IsUniqueConstraintViolation(_strategy))
                {
                    // If we have a duplicate key exception, then the snapshot has already been created
                }
            }
        }
예제 #2
0
        public async Task SetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            SerializedSnapshot serializedSnapshot,
            CancellationToken cancellationToken)
        {
            var msSqlSnapshotDataModel = new MsSqlSnapshotDataModel
            {
                AggregateId             = identity.Value,
                AggregateName           = aggregateType.GetAggregateName().Value,
                AggregateSequenceNumber = serializedSnapshot.Metadata.AggregateSequenceNumber,
                Metadata = serializedSnapshot.SerializedMetadata,
                Data     = serializedSnapshot.SerializedData,
            };

            try
            {
                await _msSqlConnection.ExecuteAsync(
                    Label.Named("set-snapshot"),
                    cancellationToken,
                    @"INSERT INTO [dbo].[EventFlowSnapshots]
                        (AggregateId, AggregateName, AggregateSequenceNumber, Metadata, Data)
                        VALUES
                        (@AggregateId, @AggregateName, @AggregateSequenceNumber, @Metadata, @Data)",
                    msSqlSnapshotDataModel)
                .ConfigureAwait(false);
            }
            catch (SqlException sqlException) when(sqlException.Number == 2601)
            {
                // If we have a duplicate key exception, then the snapshot has already been created
            }
        }
예제 #3
0
        public async Task SetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            SerializedSnapshot serializedSnapshot,
            CancellationToken cancellationToken)
        {
            var mongoDbSnapshotDataModel = new MongoDbSnapshotDataModel
            {
                _id                     = ObjectId.GenerateNewId(DateTime.UtcNow),
                AggregateId             = identity.Value,
                AggregateName           = aggregateType.GetAggregateName().Value,
                AggregateSequenceNumber = serializedSnapshot.Metadata.AggregateSequenceNumber,
                Metadata                = serializedSnapshot.SerializedMetadata,
                Data                    = serializedSnapshot.SerializedData,
            };

            var collection    = _mongoDatabase.GetCollection <MongoDbSnapshotDataModel>(SnapShotsCollectionName);
            var filterBuilder = Builders <MongoDbSnapshotDataModel> .Filter;

            var filter = filterBuilder.Eq(model => model.AggregateName, aggregateType.GetAggregateName().Value) &
                         filterBuilder.Eq(model => model.AggregateId, identity.Value) &
                         filterBuilder.Eq(model => model.AggregateSequenceNumber, serializedSnapshot.Metadata.AggregateSequenceNumber);

            await collection.DeleteManyAsync(filter, cancellationToken);

            await collection.InsertOneAsync(mongoDbSnapshotDataModel, cancellationToken : cancellationToken);
        }
예제 #4
0
        public async Task SetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            SerializedSnapshot serializedSnapshot,
            CancellationToken cancellationToken)
        {
            var postgreSqlSnapshotDataModel = new PostgreSqlSnapshotDataModel
            {
                AggregateId             = identity.Value,
                AggregateName           = aggregateType.GetAggregateName().Value,
                AggregateSequenceNumber = serializedSnapshot.Metadata.AggregateSequenceNumber,
                Metadata = serializedSnapshot.SerializedMetadata,
                Data     = serializedSnapshot.SerializedData,
            };

            try
            {
                await _postgreSqlConnection.ExecuteAsync(
                    Label.Named("set-snapshot"),
                    cancellationToken,
                    @"INSERT INTO EventFlowSnapshots
                        (AggregateId, AggregateName, AggregateSequenceNumber, Metadata, Data)
                        VALUES
                        (@AggregateId, @AggregateName, @AggregateSequenceNumber, @Metadata, @Data);",
                    postgreSqlSnapshotDataModel)
                .ConfigureAwait(false);
            }
            catch (PostgresException sqlException) when(sqlException.SqlState == "23505")
            {
                //If we have a duplicate key exception, then the snapshot has already been created
                //https://www.postgresql.org/docs/9.4/static/errcodes-appendix.html

                _log.Debug("Duplicate key SQL exception : {0}", sqlException.MessageText);
            }
        }
예제 #5
0
        public Task SetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            SerializedSnapshot serializedSnapshot,
            CancellationToken cancellationToken)
        {
            _log.Warning($"Trying to store aggregate snapshot '{aggregateType.PrettyPrint()}' with ID '{identity}' in the NULL store. Configure another store!");

            return(Task.FromResult(0));
        }
        public Task SetSnapshotAsync(Type aggregateType, IIdentity identity, SerializedSnapshot serializedSnapshot, CancellationToken cancellationToken)
        {
            var snapshot = new DynamoDBSnapshot()
            {
                AggregateId             = identity.Value,
                AggregateName           = aggregateType.GetAggregateName().Value,
                AggregateSequenceNumber = serializedSnapshot.Metadata.AggregateSequenceNumber,
                Metadata = serializedSnapshot.SerializedMetadata,
                Data     = serializedSnapshot.SerializedData
            };

            return(_dynamoDBContext.SaveAsync(snapshot, cancellationToken));
        }
예제 #7
0
        public Task SetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            SerializedSnapshot serializedSnapshot,
            CancellationToken cancellationToken)
        {
            _logger.LogWarning(
                "Trying to store aggregate snapshot {AggregateType} with ID {Id} in the NULL store. Configure another store!",
                aggregateType.PrettyPrint(),
                identity);

            return(Task.CompletedTask);
        }
        public async Task SetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            SerializedSnapshot serializedSnapshot,
            CancellationToken cancellationToken)
        {
            using (await _asyncLock.WaitAsync(cancellationToken).ConfigureAwait(false)) {
                _log.Verbose(() => $"Setting snapshot '{aggregateType.PrettyPrint()}' with ID '{identity.Value}'");

                Dictionary <string, CommittedSnapshot> snapshots;
                if (!_snapshots.TryGetValue(aggregateType, out snapshots))
                {
                    snapshots = new Dictionary <string, CommittedSnapshot>();
                    _snapshots[aggregateType] = snapshots;
                }

                snapshots[identity.Value] = serializedSnapshot;
            }
        }
        public async Task SetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            SerializedSnapshot serializedSnapshot,
            CancellationToken cancellationToken)
        {
            using (await _asyncLock.WaitAsync(cancellationToken).ConfigureAwait(false))
            {
                _logger.LogTrace(
                    "Setting snapshot {AggregateType} with ID {Id}",
                    aggregateType.PrettyPrint(),
                    identity.Value);

                if (!_snapshots.TryGetValue(aggregateType, out var snapshots))
                {
                    snapshots = new Dictionary <string, CommittedSnapshot>();
                    _snapshots[aggregateType] = snapshots;
                }

                snapshots[identity.Value] = serializedSnapshot;
            }
        }