Пример #1
0
 /// <summary>
 /// Deprecated Method for adding a new object to the AccountBalances EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAccountBalances(AccountBalance accountBalance)
 {
     base.AddObject("AccountBalances", accountBalance);
 }
Пример #2
0
 /// <summary>
 /// Deprecated Method for adding a new object to the AccountBalances EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAccountBalances(AccountBalance accountBalance)
 {
     base.AddObject("AccountBalances", accountBalance);
 }
Пример #3
0
 /// <summary>
 /// Create a new AccountBalance object.
 /// </summary>
 /// <param name="account">Initial value of the Account property.</param>
 /// <param name="dateOfBusiness">Initial value of the DateOfBusiness property.</param>
 /// <param name="balance">Initial value of the Balance property.</param>
 public static AccountBalance CreateAccountBalance(global::System.String account, global::System.DateTime dateOfBusiness, global::System.Decimal balance)
 {
     AccountBalance accountBalance = new AccountBalance();
     accountBalance.Account = account;
     accountBalance.DateOfBusiness = dateOfBusiness;
     accountBalance.Balance = balance;
     return accountBalance;
 }
Пример #4
0
        private void ThreadProc()
        {
            while (!_stopping.WaitOne(1000))
            {
                try
                {
                    // TODO 3e: Background process inserts snapshots.
                    using (CAP_EventSourcingEntities entities = new CAP_EventSourcingEntities())
                    {
                        DateTime dateOfBusinees  = entities.Current.Single().DateOfBusiness;
                        DateTime lastBalanceDate = dateOfBusinees - TimeSpan.FromDays(2.0);
                        DateTime thisBalanceDate = dateOfBusinees - TimeSpan.FromDays(1.0);

                        // Get a balance that needs to be calculated.
                        AccountBalance lastBalance = entities.AccountBalances
                                                     .Where(l =>
                                                            l.DateOfBusiness == lastBalanceDate &&
                                                            !entities.AccountBalances.Any(t =>
                                                                                          t.Account == l.Account &&
                                                                                          t.DateOfBusiness == thisBalanceDate)).FirstOrDefault();
                        if (lastBalance == null)
                        {
                            if (!_wroteEmptyMessage)
                            {
                                Console.WriteLine("Snapshots up to date.");
                            }
                            _wroteEmptyMessage = true;
                        }
                        else
                        {
                            Console.WriteLine(String.Format("Updating account balance for {0} on {1}.",
                                                            lastBalance.Account,
                                                            thisBalanceDate));
                            _wroteEmptyMessage = false;

                            // Calculate the balance.
                            var transfersFrom =
                                from t in entities.Transfers
                                where
                                t.From == lastBalance.Account &&
                                t.DateOfBusiness > lastBalanceDate &&
                                t.DateOfBusiness <= thisBalanceDate
                                select(decimal?) t.Amount;

                            var transfersTo =
                                from t in entities.Transfers
                                where
                                t.To == lastBalance.Account &&
                                t.DateOfBusiness > lastBalanceDate &&
                                t.DateOfBusiness <= thisBalanceDate
                                select(decimal?) t.Amount;

                            var thisBalance = lastBalance.Balance - (transfersFrom.Sum() ?? 0.0m) + (transfersTo.Sum() ?? 0.0m);

                            // Insert the new snapshot.
                            entities.AddToAccountBalances(new AccountBalance()
                            {
                                Account        = lastBalance.Account,
                                DateOfBusiness = thisBalanceDate,
                                Balance        = thisBalance
                            });

                            entities.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }