public async Task SaveSnapshotAsync(IAggregateRoot aggregateRoot, Type aggregateRootType, int publishedVersion) { if (aggregateRoot == null) { throw new ArgumentNullException(nameof(aggregateRoot)); } if (publishedVersion % _aggregateSnapshotConfiguration.VersionInterval != 0) { return; } var copiedAggregateRoot = DeepCopier.Copy(aggregateRoot); var aggregateRootJson = _aggregateSnapshotSerializer.Serialize(copiedAggregateRoot); var aggregateRootTypeName = _typeNameProvider.GetTypeName(aggregateRootType); var snapshot = new Snapshot() { CreationTime = DateTime.UtcNow, ModificationTime = DateTime.UtcNow, AggregateRootId = copiedAggregateRoot.UniqueId, AggregateRootTypeName = aggregateRootTypeName, Version = copiedAggregateRoot.Version, SerializedPayload = aggregateRootJson, }; var sql = string.Format(InsertOrUpdateSnapshotSql, _snapshotRepository.GetTableName(snapshot.AggregateRootId)); using (var connection = _snapshotRepository.GetConnection()) { await connection.ExecuteAsync(sql, snapshot); } }
public async Task CreateOrUpdateSnapshotPayloadAsync(string aggregateRootId, string aggregateRootTypeName, int publishedVersion, string payload) { var snapshot = new Snapshot() { CreationTime = DateTime.UtcNow, ModificationTime = DateTime.UtcNow, AggregateRootId = aggregateRootId, AggregateRootTypeName = aggregateRootTypeName, Version = publishedVersion, SerializedPayload = payload, }; var sql = string.Format(InsertOrUpdateSnapshotSql, _snapshotRepository.GetTableName(snapshot.AggregateRootId)); using (var connection = _snapshotRepository.GetConnection()) { await connection.OpenAsync(); await connection.ExecuteAsync(sql, snapshot); } }
public async Task <IAggregateRoot> RestoreFromSnapshotAsync(Type aggregateRootType, string aggregateRootId) { var snapshot = default(Snapshot); using (var connection = _snapshotRepository.GetConnection()) { var result = await connection.QueryListAsync <Snapshot>(new { AggregateRootId = aggregateRootId }, _snapshotRepository.GetTableName(aggregateRootId)); snapshot = result.SingleOrDefault(); } if (snapshot == null) { return(null); } return(_aggregateSnapshotSerializer.Deserialize(snapshot.SerializedPayload, aggregateRootType) as IAggregateRoot); }