예제 #1
0
        // TODO 1c: Transfer funds within a transaction.
        public void Transfer(string fromAccount, string toAccount, decimal amount)
        {
            try
            {
                using (CAP_CentralDatabaseEntities entities = new CAP_CentralDatabaseEntities())
                {
                    using (var tx = new TransactionScope())
                    {
                        AccountBalance from = GetAccountEntity(fromAccount, entities);
                        AccountBalance to   = GetAccountEntity(toAccount, entities);

                        if (from.Balance < amount)
                        {
                            throw new ApplicationException("Insufficient funds.");
                        }

                        from.Balance -= amount;
                        to.Balance   += amount;

                        entities.SaveChanges();
                        tx.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
예제 #2
0
        // TODO 1c: Transfer funds within a transaction.
        public void Transfer(string fromAccount, string toAccount, decimal amount)
        {
            try
            {
                using (CAP_CentralDatabaseEntities entities = new CAP_CentralDatabaseEntities())
                {
                    using (var tx = new TransactionScope())
                    {
                        AccountBalance from = GetAccountEntity(fromAccount, entities);
                        AccountBalance to = GetAccountEntity(toAccount, entities);

                        if (from.Balance < amount)
                            throw new ApplicationException("Insufficient funds.");

                        from.Balance -= amount;
                        to.Balance += amount;

                        entities.SaveChanges();
                        tx.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
예제 #3
0
 private static AccountBalance GetAccountEntity(string account, CAP_CentralDatabaseEntities entities)
 {
     IQueryable<AccountBalance> query =
         from b in entities.AccountBalances
         where b.Account == account
         select b;
     AccountBalance entity = query.FirstOrDefault();
     if (entity == null)
         throw new ApplicationException("No such account.");
     return entity;
 }
예제 #4
0
        private static AccountBalance GetAccountEntity(string account, CAP_CentralDatabaseEntities entities)
        {
            IQueryable <AccountBalance> query =
                from b in entities.AccountBalances
                where b.Account == account
                select b;
            AccountBalance entity = query.FirstOrDefault();

            if (entity == null)
            {
                throw new ApplicationException("No such account.");
            }
            return(entity);
        }
예제 #5
0
 // TODO 1b: Query for the account balance.
 public decimal GetAccountBalance(string account)
 {
     try
     {
         using (CAP_CentralDatabaseEntities entities = new CAP_CentralDatabaseEntities())
         {
             AccountBalance entity = GetAccountEntity(account, entities);
             return(entity.Balance);
         }
     }
     catch (Exception ex)
     {
         throw new FaultException(ex.Message);
     }
 }
예제 #6
0
 // TODO 1b: Query for the account balance.
 public decimal GetAccountBalance(string account)
 {
     try
     {
         using (CAP_CentralDatabaseEntities entities = new CAP_CentralDatabaseEntities())
         {
             AccountBalance entity = GetAccountEntity(account, entities);
             return entity.Balance;
         }
     }
     catch (Exception ex)
     {
         throw new FaultException(ex.Message);
     }
 }