Example #1
0
 public void Apply(AccountingMonthOpened @event)
 {
     Id           = @event.FinancialAccountId;
     CurrentMonth = @event.Month;
     CurrentYear  = @event.Year;
     Balance      = @event.StartingBalance;
     IsOpened     = true;
     Version++;
 }
Example #2
0
        public async Task AggregatingStreamIntoShouldUseState()
        {
            var financialAccountId = Guid.NewGuid();

            await AppendEvent(
                financialAccountId,
                new AccountingMonthOpened(financialAccountId, 10, 2021, 0),
                new InflowRecorded(financialAccountId, 100),
                new InflowRecorded(financialAccountId, 100),
                new InflowRecorded(financialAccountId, 100),
                new AccountingMonthClosed(financialAccountId, 10, 2021, 300)
                );

            #region sample_aggregate-stream-into-state-store

            (FinancialAccount, AccountingMonthOpened) OpenAccountingMonth(
                FinancialAccount cashRegister)
            {
                var @event = new AccountingMonthOpened(
                    cashRegister.Id, 11, 2021, 300);

                cashRegister.Apply(@event);
                return(cashRegister, @event);
            }

            var closedCashierShift =
                await theSession.Events.AggregateStreamAsync <FinancialAccount>(
                    financialAccountId
                    );

            var(openedCashierShift, cashierShiftOpened) =
                OpenAccountingMonth(closedCashierShift !);

            var repository = new CashRegisterRepository(_session);

            await repository.Store(openedCashierShift, cashierShiftOpened);

            #endregion

            var snapshot = await theSession.LoadAsync <FinancialAccount>(financialAccountId);

            snapshot.ShouldNotBeNull();
            snapshot.Id.ShouldBe(financialAccountId);
            snapshot.CurrentMonth.ShouldBe(11);
            snapshot.CurrentYear.ShouldBe(2021);
            snapshot.Balance.ShouldBe(300);
            snapshot.IsOpened.ShouldBeTrue();
            snapshot.Version.ShouldBe(openedCashierShift.Version);

            await AppendEvent(
                financialAccountId,
                new InflowRecorded(financialAccountId, 100),
                new InflowRecorded(financialAccountId, 100),
                new InflowRecorded(financialAccountId, 100)
                );

            #region sample_aggregate-stream-into-state-get

            var currentState = await repository.Get(financialAccountId);

            #endregion

            currentState.ShouldNotBeNull();
            currentState.Id.ShouldBe(financialAccountId);
            snapshot.CurrentMonth.ShouldBe(11);
            snapshot.CurrentYear.ShouldBe(2021);
            currentState.Balance.ShouldBe(600);
            currentState.IsOpened.ShouldBeTrue();
            currentState.Version.ShouldBe(openedCashierShift.Version + 3);
        }