public async Task <IExecutionResult> TakeSnapshotAsync(string stream) { Optional <TAggregateRoot> root = await _rootRepository.GetAsync(stream); root.ThrowsIf(r => !r.HasValue, new AggregateNotFoundException(stream)); Aggregate aggregate; this._changeTracker.TryGet(stream, out aggregate); var changes = new EventData( Guid.NewGuid(), typeof(TSnapshot).TypeQualifiedName(), true, Encoding.UTF8.GetBytes(this._eventSerializer.Serialize(((ISnapshotable)root.Value).TakeSnapshot())), Encoding.UTF8.GetBytes(this._eventSerializer.Serialize(new EventMetadata { AggregateAssemblyQualifiedName = typeof(TAggregateRoot).AssemblyQualifiedName, AggregateType = typeof(TAggregateRoot).Name, TimeStamp = DateTime.Now, IsSnapshot = true, Version = Convert.ToInt32(aggregate.ExpectedVersion) }) )); var snapShotStreamName = _streamNameProvider.GetSnapshotStreamName(root, stream); await _connection.AppendToStreamAsync(snapShotStreamName, ExpectedVersion.Any, changes); return(ExecutionResult.Success); }
public async Task should_not_allow_to_update_if_the_entity_has_changed() { var existingCar1 = await _aggregateRootRepository.GetAsync(_fakeCarId); var existingCar2 = await _aggregateRootRepository.GetAsync(_fakeCarId); var result = await WithUnitOfWorkAsync(async() => { Car.ChangeName(existingCar2.Value, "Changed-2"); }); result.IsSuccess.Should().BeTrue(); Func <Task> func = async() => { await WithUnitOfWorkAsync(async() => { Car.ChangeName(existingCar1.Value, "Changed-1"); }); }; func.Should().Throw <Exception>(); }
public async Task <IExecutionResult> TakeSnapshotAsync(string stream) { Optional <TAggregateRoot> root = await _rootRepository.GetAsync(stream); root.ThrowsIf(r => !r.HasValue, new AggregateNotFoundException(stream)); _changeTracker.TryGet(stream, out Aggregate aggregate); var serializedState = _eventSerializer.Serialize(root.Value.TakeSnapshot()); var newSnapshot = new SnapshotDocument(aggregate.Identifier, serializedState, aggregate.ExpectedVersion.Value.ToString(), null, typeof(TSnapshot).TypeQualifiedName()); await _cosmosDbClient.CreateItemAsync(newSnapshot, _cosmosDBConfigurations.SnapshotContainerName).ConfigureAwait(false); return(ExecutionResult.Success); }
public async Task concurrent_append_to_stream_events_should_be_ordered() { var existingCar = await _aggregateRootRepository.GetAsync(_fakeCarId); var taskList = new Task[30]; for (int i = 0; i < 30; i++) { taskList[i] = Task.Run(() => { try { WithUnitOfWorkAsync(async() => { Car.ChangeName(existingCar.Value, $"Fake-{i.ToString()}"); }).ConfigureAwait(false).GetAwaiter().GetResult(); } catch { } }); } await Task.WhenAll(taskList); var streamId = _streamNameProvider.GetStreamName(typeof(CarState), _fakeCarId); var streamEvents = await _cosmosDBConnection.ReadStreamEventsForwardAsync(streamId, 1, 1000); var eventCount = streamEvents.Value.Events.Length; var distinctedCount = streamEvents.Value.Events.ToList().Select(e => e.Position) .Distinct() .Count(); eventCount.Should().Be(distinctedCount); }
public async Task get_existing_record_from_repository_shuold_return_record() { var existingCar = await _aggregateRootRepository.GetAsync(_fakeCarId); existingCar.Should().NotBeNull(); }
public async Task get_existing_record_from_repository_shuold_return_record() { var existingOrder = await _aggregateRootRepository.GetAsync(FAKE_ORDER_ID); existingOrder.Should().NotBeNull(); }