/// <inheritdoc /> public async Task <Snapshot> ReadSnapshot(object id, Type aggregateType) { var snapshot = await _connection.ReadEventAsync(StreamNameResolver.ResolveForSnapshot(id, aggregateType), StreamPosition.End, true); if (NotAnySnapshotExists(snapshot)) { return(await Task.FromResult <Snapshot>(null)); } return((Snapshot)JsonConvert.DeserializeObject( // ReSharper disable once PossibleInvalidOperationException Encoding.UTF8.GetString(snapshot.Event.Value.Event.Data), TypeResolver.ResolveSnapshotType(snapshot.Event.Value.Event.EventType))); }
/// <inheritdoc /> public async Task TakeSnapshotIfNeed(IAggregateRoot aggregate) { var lastSnapshotVersion = -1L; var lastSnapshot = await _connection.ReadEventAsync(StreamNameResolver.ResolveForSnapshot(aggregate), StreamPosition.End, true); if (AnySnapshotExists(lastSnapshot)) { lastSnapshotVersion = lastSnapshot.Event.GetValueOrDefault().OriginalEventNumber; } if (NeedToTakeSnapshot(aggregate, lastSnapshotVersion)) { await TakeSnapshot(_connection, aggregate); } }