public Task<SerializedSnapshot> SerilizeAsync<TAggregate, TIdentity, TSnapshot>(
            SnapshotContainer snapshotContainer,
            CancellationToken cancellationToken)
            where TAggregate : ISnapshotAggregateRoot<TIdentity, TSnapshot>
            where TIdentity : IIdentity
            where TSnapshot : ISnapshot {
            var snapsnotDefinition = _snapshotDefinitionService.GetDefinition(typeof(TSnapshot));

            _log.Verbose(
                () =>
                    $"Building snapshot '{snapsnotDefinition.Name}' v{snapsnotDefinition.Version} for {typeof(TAggregate).PrettyPrint()}");

            var updatedSnapshotMetadata =
                new SnapshotMetadata(snapshotContainer.Metadata.Concat(new Dictionary<string, string> {
                    {SnapshotMetadataKeys.SnapshotName, snapsnotDefinition.Name},
                    {SnapshotMetadataKeys.SnapshotVersion, snapsnotDefinition.Version.ToString()},
                }));

            var serializedMetadata = _jsonSerializer.Serialize(updatedSnapshotMetadata);
            var serializedData = _jsonSerializer.Serialize(snapshotContainer.Snapshot);

            return Task.FromResult(new SerializedSnapshot(
                serializedMetadata,
                serializedData,
                updatedSnapshotMetadata));
        }
        private Task LoadSnapshotContainerAsync(SnapshotContainer snapshotContainer, CancellationToken cancellationToken)
        {
            if (SnapshotVersion.HasValue)
            {
                throw new InvalidOperationException(
                          $"Aggregate '{Id}' of type '{GetType().PrettyPrint()}' already has snapshot loaded");
            }
            if (Version > 0)
            {
                throw new InvalidOperationException(
                          $"Aggregate '{Id}' of type '{GetType().PrettyPrint()}' already has events loaded");
            }
            if (!(snapshotContainer.Snapshot is TSnapshot))
            {
                throw new ArgumentException(
                          $"Snapshot '{snapshotContainer.Snapshot.GetType().PrettyPrint()}' for aggregate '{GetType().PrettyPrint()}' is not of type '{typeof(TSnapshot).PrettyPrint()}'. Did you forget to implement a snapshot upgrader?");
            }

            SnapshotVersion = snapshotContainer.Metadata.AggregateSequenceNumber;

            return(LoadSnapshotAsync(
                       (TSnapshot)snapshotContainer.Snapshot,
                       snapshotContainer.Metadata,
                       cancellationToken));
        }
        private async Task <SnapshotContainer> CreateSnapshotContainerAsync(CancellationToken cancellationToken)
        {
            var snapshotTask         = CreateSnapshotAsync(cancellationToken);
            var snapshotMetadataTask = CreateSnapshotMetadataAsync(cancellationToken);

            await Task.WhenAll(snapshotTask, snapshotMetadataTask).ConfigureAwait(false);

            var snapshotContainer = new SnapshotContainer(
                snapshotTask.Result,
                snapshotMetadataTask.Result);

            return(snapshotContainer);
        }
Example #4
0
        public async Task StoreSnapshotAsync <TAggregate, TIdentity, TSnapshot>(
            TIdentity identity,
            SnapshotContainer snapshotContainer,
            CancellationToken cancellationToken)
            where TAggregate : ISnapshotAggregateRoot <TIdentity, TSnapshot>
            where TIdentity : IIdentity
            where TSnapshot : ISnapshot
        {
            var serializedSnapshot = await _snapshotSerilizer.SerilizeAsync <TAggregate, TIdentity, TSnapshot>(
                snapshotContainer,
                cancellationToken)
                                     .ConfigureAwait(false);

            await _snapshotPersistence.SetSnapshotAsync(
                typeof(TAggregate),
                identity,
                serializedSnapshot,
                cancellationToken)
            .ConfigureAwait(false);
        }