public async Task SaveAndRestoreSnapshot_Tests() { //Arrange var product = new Product ( 1L, "tomato", true, new List <long>() { 1L, 2L, 3L }, new List <ProductRecord>() { new ProductRecord(1L, "a", DateTime.UtcNow), new ProductRecord(2L, "b", DateTime.UtcNow), new ProductRecord(3L, "c", DateTime.UtcNow), new ProductRecord(4L, "d", DateTime.UtcNow), } ); //Act await _savableAggregateSnapshotter.SaveSnapshotAsync(product, typeof(Product), (product as IAggregateRoot).Version); var restoredProduct = (await _aggregateSnapshotter.RestoreFromSnapshotAsync(typeof(Product), product.Id.ToString())) as Product; //Assert restoredProduct.Id.ShouldBe(product.Id); restoredProduct.Name.ShouldBe(product.Name); restoredProduct.IsPublished.ShouldBe(product.IsPublished); }
private async Task <IAggregateRoot> TryGetFromSnapshot(string aggregateRootId, Type aggregateRootType) { var aggregateRoot = await _aggregateSnapshotter.RestoreFromSnapshotAsync(aggregateRootType, aggregateRootId).ConfigureAwait(false); if (aggregateRoot == null) { return(null); } if (aggregateRoot.GetType() != aggregateRootType || aggregateRoot.UniqueId != aggregateRootId) { throw new Exception(string.Format("AggregateRoot recovery from snapshot is invalid as the aggregateRootType or aggregateRootId is not matched. Snapshot: [aggregateRootType:{0},aggregateRootId:{1}], expected: [aggregateRootType:{2},aggregateRootId:{3}]", aggregateRoot.GetType(), aggregateRoot.UniqueId, aggregateRootType, aggregateRootId)); } var aggregateRootTypeName = _typeNameProvider.GetTypeName(aggregateRootType); var taskResult = await _eventStore.QueryAggregateEventsAsync(aggregateRootId, aggregateRootTypeName, aggregateRoot.Version + 1, int.MaxValue).ConfigureAwait(false); if (taskResult.Status == AsyncTaskStatus.Success) { aggregateRoot.ReplayEvents(taskResult.Data); return(aggregateRoot); } return(null); }
private Task <IAggregateRoot> TryRestoreFromSnapshotAsync(Type aggregateRootType, string aggregateRootId, int retryTimes, TaskCompletionSource <IAggregateRoot> taskSource) { _ioHelper.TryAsyncActionRecursively("TryRestoreFromSnapshotAsync", () => _aggregateSnapshotter.RestoreFromSnapshotAsync(aggregateRootType, aggregateRootId), currentRetryTimes => TryRestoreFromSnapshotAsync(aggregateRootType, aggregateRootId, currentRetryTimes, taskSource), result => { taskSource.SetResult(result); }, () => string.Format("_aggregateSnapshotter.TryRestoreFromSnapshotAsync has unknown exception, aggregateRootType: {0}, aggregateRootId: {1}", aggregateRootType.FullName, aggregateRootId), null, retryTimes, true); return(taskSource.Task); }
public async Task SaveAndRestoreSnapshot_Tests() { //Arrange var bigName = new StringBuilder(); for (int i = 0; i < 100000; i++) { bigName.Append("name"); } var product = new Product ( 1L, "tomato", true, new List <long>() { 1L, 2L, 3L }, new List <ProductRecord>() { new ProductRecord(1L, bigName.ToString(), DateTime.UtcNow), new ProductRecord(2L, "b", DateTime.UtcNow), new ProductRecord(3L, "c", DateTime.UtcNow), new ProductRecord(4L, "d", DateTime.UtcNow), } ); //Act await _aggregateSnapshotSaver.SaveAsync(product, typeof(Product), (product as IAggregateRoot).Version); await Task.Delay(1500); var restoredProduct = (await _aggregateSnapshotter.RestoreFromSnapshotAsync(typeof(Product), product.Id.ToString())) as Product; //Assert restoredProduct.Id.ShouldBe(product.Id); restoredProduct.Name.ShouldBe(product.Name); restoredProduct.IsPublished.ShouldBe(product.IsPublished); }
public async Task <IAggregateRoot> GetAsync(Type aggregateRootType, string aggregateRootId) { if (aggregateRootType == null) { throw new ArgumentNullException("aggregateRootType"); } if (aggregateRootId == null) { throw new ArgumentNullException("aggregateRootId"); } var aggregateRoot = await _aggregateSnapshotter.RestoreFromSnapshotAsync(aggregateRootType, aggregateRootId).ConfigureAwait(false); if (aggregateRoot != null && (aggregateRoot.GetType() != aggregateRootType || aggregateRoot.UniqueId != aggregateRootId)) { throw new Exception(string.Format("AggregateRoot recovery from snapshot is invalid as the aggregateRootType or aggregateRootId is not matched. Snapshot: [aggregateRootType:{0},aggregateRootId:{1}], expected: [aggregateRootType:{2},aggregateRootId:{3}]", aggregateRoot.GetType(), aggregateRoot.UniqueId, aggregateRootType, aggregateRootId)); } return(aggregateRoot); }