public async Task RunBankingScenario() { var accountId = Guid.NewGuid(); var repo = await ProviderFactory.Current.CreateRepositoryAsync() .ConfigureAwait(false); var handler = new BankAccountCommandHandlers(repo); const string joeBloggs = "Joe Bloggs"; await handler.Handle(CreateAccountCommand.Create(Guid.NewGuid(), accountId, joeBloggs)).ConfigureAwait(false); await handler.Handle(DepositFundsCommand.Create(Guid.NewGuid(), accountId, 10)).ConfigureAwait(false); await handler.Handle(DepositFundsCommand.Create(Guid.NewGuid(), accountId, 35)).ConfigureAwait(false); await handler.Handle(WithdrawFundsCommand.Create(Guid.NewGuid(), accountId, 25)).ConfigureAwait(false); await handler.Handle(DepositFundsCommand.Create(Guid.NewGuid(), accountId, 5)).ConfigureAwait(false); await handler.Handle(WithdrawFundsCommand.Create(Guid.NewGuid(), accountId, 10)).ConfigureAwait(false); var actual = await repo.GetByIdAsync <BankAccount>(accountId).ConfigureAwait(false); actual.Id.Should().Be(accountId); actual.Name.Should().Be(joeBloggs); actual.CurrentBalance.Should().Be(15); actual.Transactions.Count.Should().Be(5); }
public void Commands_cant_have_empty_aggregate_id() { Action act = () => { var command = new DepositFundsCommand(Guid.NewGuid(), Guid.Empty, 100); }; act.ShouldThrow <ArgumentException>(); }
public void Commands_cant_have_empty_correlation_tokens() { Action act = () => { var command = new DepositFundsCommand(Guid.Empty, Guid.NewGuid(), 100); }; act.ShouldThrow <ArgumentException>(); }
public async Task HandleAsync(DepositFundsCommand command) { var account = await _repository.GetByIdAsync <BankAccount>(command.AggregateId) .ConfigureAwait(false); account.Deposit(command.Amount, command.CorrelationId); await _repository.SaveAsync(account) .ConfigureAwait(false); }
public async Task Handle(DepositFundsCommand command) { Log.Information("Handling deposit command for {aggregate} correlationId:{correlationId}", command.AggregateId, command.CorrelationId); var account = await _repo.GetByIdAsync <BankAccount>(command.AggregateId) .ConfigureAwait(false); account.Deposit(command.Amount, command.CorrelationId); await _repo.SaveAsync(account) .ConfigureAwait(false); }
private static void Main() { var log = new LoggerConfiguration() .WriteTo.ColoredConsole(outputTemplate: "{Timestamp:HH:mm} [{Level}] ({Name:l}) {Message}{NewLine}{Exception}") .MinimumLevel.Debug() .CreateLogger(); Log.Logger = log; log.Information("Event sourcing sample"); var accountId = Guid.NewGuid(); var providerFactory = ProviderFactory.Current; var repo = providerFactory.CreateRepositoryAsync().Result; log.Information("Initialising provider"); providerFactory.InitAsync().Wait(); log.Information("Provider initialised"); log.Information("Tearing down provider"); var cleaner = providerFactory.CreateTeardownAsync().Result; cleaner.TearDownAsync().Wait(); log.Information("StorageProviderFactory torn down"); var handler = new BankAccountCommandHandlers(repo); log.Information("Running set of commands"); handler.Handle(CreateAccountCommand.Create(Guid.NewGuid(), accountId, "Joe Bloggs")).Wait(); handler.Handle(DepositFundsCommand.Create(Guid.NewGuid(), accountId, 10)).Wait(); handler.Handle(DepositFundsCommand.Create(Guid.NewGuid(), accountId, 35)).Wait(); handler.Handle(WithdrawFundsCommand.Create(Guid.NewGuid(), accountId, 25)).Wait(); handler.Handle(DepositFundsCommand.Create(Guid.NewGuid(), accountId, 5)).Wait(); handler.Handle(WithdrawFundsCommand.Create(Guid.NewGuid(), accountId, 10)).Wait(); log.Information("Commands Run"); log.Information("Get aggregate from store"); var fromStore = repo.GetByIdAsync <BankAccount>(accountId).Result; log.Information($"Bank account ID: {fromStore.Id}"); log.Information($"Balance: {fromStore.CurrentBalance}"); log.Information($"Last committed version: {fromStore.LastCommittedVersion}"); log.Information($"Transaction Count: {fromStore.Transactions.Count}"); log.Information("Event sourcing sample ran"); System.Console.ReadLine(); }
public Task Handle(Command message) { _client.Send(Resources.BankAccountQueueName, DepositFundsCommand.Create(message.CorrelationId, message.AccountId, message.Amount)); return(Task.CompletedTask); }
public void Commands_can_be_constructed() { var depositFundsCommand = new DepositFundsCommand(Guid.NewGuid(), Guid.NewGuid(), 100); }