private static async Task MainAsync() { var command = new CreateFooCommand() { Data = "Foobar", IssuedAt = DateTime.UtcNow }; await _bus.Send(command); Console.WriteLine("Press any key to simulate a command that throws exception"); Console.ReadKey(); var badCommand = new CreateBarThatThrowsCommand() { Stuff = "Bad Stuff" }; try { await _bus.Send(badCommand); } catch (Exception exception) { Console.WriteLine(exception.StackTrace); } Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
public async Task <IActionResult> Post() { var cmd = new CreateFooCommand(Guid.NewGuid()); await _dispatcher.DispatchAsync(cmd); await _unitOfWork.CommitAsync(); return(Ok(cmd)); }
public async Task <IActionResult> Post() { var cmd = new CreateFooCommand(Guid.NewGuid()); var response = await _dispatcher.DispatchAsync <CreateFooCommand, CreateFooResponse>(cmd); await _session.CommitAsync(); return(Ok(cmd)); }
public async Task <IActionResult> DoFlood(int times) { var create = new CreateFooCommand(Guid.NewGuid()); await _dispatcher.DispatchAsync(create); var aggregate = await _repository.GetByIdAsync <Foo>(create.AggregateId); for (var i = 1; i < times; i++) { aggregate.DoSomething(); } await _unitOfWork.CommitAsync(); return(Ok(create)); }
public async Task <IActionResult> DoFlood(int times) { var create = new CreateFooCommand(Guid.NewGuid()); var response = await _dispatcher.DispatchAsync <CreateFooCommand, CreateFooResponse>(create); var stream = await _repository.GetByIdAsync <Foo>(create.AggregateId); for (var i = 1; i < times; i++) { stream.DoSomething(); } await _session.CommitAsync(); return(Ok(create)); }
public async Task Should_dispatch_command_and_retrieve_aggregate_from_repository() { using (var scope = _fixture.Container.BeginLifetimeScope()) { var command = new CreateFooCommand(Guid.NewGuid()); DoDispatch(scope, command); var repository = scope.Resolve <IRepository>(); var aggregateFromRepository = await repository.GetByIdAsync <Foo>(command.AggregateId).ConfigureAwait(false); aggregateFromRepository.Should().NotBeNull(); aggregateFromRepository.Id.Should().Be(command.AggregateId); } }
public async Task Should_take_and_restore_snapshot_based_on_interval_strategy_configured() { const int times = 5; _fixture.SnapshotStrategy = new IntervalSnapshotStrategy(times + 1); var aggregateId = Guid.NewGuid(); var command = new CreateFooCommand(aggregateId); using (var scope = _fixture.Container.BeginLifetimeScope()) { DoDispatch(scope, command); _fixture.EventStore.CalledMethods.HasFlag(EventStoreMethods.SaveSnapshotAsync).Should().BeFalse(); } using (var scope = _fixture.Container.BeginLifetimeScope()) { DoDispatch(scope, new DoFloodSomethingCommand(aggregateId, times)); _fixture.EventStore.CalledMethods.HasFlag(EventStoreMethods.SaveSnapshotAsync).Should().BeTrue(); } using (var scope = _fixture.Container.BeginLifetimeScope()) { var repository = scope.Resolve <IRepository>(); var aggregateFromRepository = await repository.GetByIdAsync <Foo>(aggregateId).ConfigureAwait(false); aggregateFromRepository.Should().NotBeNull(); aggregateFromRepository.Id.Should().Be(aggregateId); // (times - 1) because already emitted create event... aggregateFromRepository.DidSomethingCounter.Should().Be(times); _fixture.EventStore.CalledMethods.HasFlag(EventStoreMethods.GetLatestSnapshotByIdAsync).Should().BeTrue(); } }
public async Task <ActionResult> CreateFooAsync([FromBody] CreateFooCommand createFooCommand, CancellationToken cancellationToken) => await HandleCommandAsync(createFooCommand, cancellationToken);