Exemple #1
0
        public async Task RunBankingScenario()
        {
            var accountId = Guid.NewGuid();

            var repo = await RepositoryFactory.CreateAsync()
                       .ConfigureAwait(false);

            var          handler   = new BankAccountCommandHandlers(repo);
            const string joeBloggs = "Joe Bloggs";

            await handler.HandleAsync(new CreateAccountCommand(Guid.NewGuid(), accountId, joeBloggs)).ConfigureAwait(false);

            await handler.HandleAsync(new DepositFundsCommand(Guid.NewGuid(), accountId, 10)).ConfigureAwait(false);

            await handler.HandleAsync(new DepositFundsCommand(Guid.NewGuid(), accountId, 35)).ConfigureAwait(false);

            await handler.HandleAsync(new WithdrawFundsCommand(Guid.NewGuid(), accountId, 25)).ConfigureAwait(false);

            await handler.HandleAsync(new DepositFundsCommand(Guid.NewGuid(), accountId, 5)).ConfigureAwait(false);

            await handler.HandleAsync(new WithdrawFundsCommand(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);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            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 repo = RepositoryFactory.CreateAsync().Result;

            log.Information("Initialising provider");
            StorageProviderInitialiser.InitAsync().Wait();
            log.Information("Provider initialised");

            log.Information("Tearing down provider");
            var cleaner = TearDownFactory.CreateAsync().Result;

            cleaner.TearDownAsync().Wait();
            log.Information("StorageProviderFactory torn down");

            var handler = new BankAccountCommandHandlers(repo);

            log.Information("Running set of commands");

            handler.HandleAsync(new CreateAccountCommand(Guid.NewGuid(), accountId, "Joe Bloggs")).Wait();
            handler.HandleAsync(new DepositFundsCommand(Guid.NewGuid(), accountId, 10)).Wait();
            handler.HandleAsync(new DepositFundsCommand(Guid.NewGuid(), accountId, 35)).Wait();
            handler.HandleAsync(new WithdrawFundsCommand(Guid.NewGuid(), accountId, 25)).Wait();
            handler.HandleAsync(new DepositFundsCommand(Guid.NewGuid(), accountId, 5)).Wait();
            handler.HandleAsync(new WithdrawFundsCommand(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();
        }