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 async Task HandleAsync(WithdrawFundsCommand command) { var account = await _repository.GetByIdAsync <BankAccount>(command.AggregateId) .ConfigureAwait(false); account.WithDrawFunds(command.Amount, command.CorrelationId); await _repository.SaveAsync(account) .ConfigureAwait(false); }
public async Task Handle(WithdrawFundsCommand message) { var account = await _repository.GetByIdAsync <BankAccount>(message.AggregateId) .ConfigureAwait(false); account.Withdraw(message.Amount, message.CorrelationId); await _repository.SaveAsync(account) .ConfigureAwait(false); }
public async Task Handle(WithdrawFundsCommand command) { Log.Information("Handling withdrawal command for {aggregate} correlationId:{correlationId}", command.AggregateId, command.CorrelationId); var account = await _repo.GetByIdAsync <BankAccount>(command.AggregateId) .ConfigureAwait(false); account.Withdraw(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, WithdrawFundsCommand.Create(message.CorrelationId, message.AccountId, message.Amount)); return(Task.CompletedTask); }