public void Transfer(double pAmount, int pFromAcctNumber, int pToAcctNumber, String pDescription) { bool lOutcome = true; String lMessage = "TransferSuccessful"; String lLogMessage; try { using (TransactionScope lScope = new TransactionScope()) using (BankEntityModelContainer lContainer = new BankEntityModelContainer()) { Account lFromAcct = GetAccountFromNumber(pFromAcctNumber); Account lToAcct = GetAccountFromNumber(pToAcctNumber); lFromAcct.Withdraw(pAmount); lToAcct.Deposit(pAmount); lContainer.Attach(lFromAcct); lContainer.Attach(lToAcct); lContainer.ObjectStateManager.ChangeObjectState(lFromAcct, System.Data.EntityState.Modified); lContainer.ObjectStateManager.ChangeObjectState(lToAcct, System.Data.EntityState.Modified); lContainer.SaveChanges(); lLogMessage = "Transfer Request Succeeded - received via MSMQ - (pAmount=" + pAmount + " , pFromAcctNumber=" + pFromAcctNumber + " , pToAcctNumber=" + pToAcctNumber + " , pDescription=" + pDescription + " , lMessage= " + lMessage + " )"; //we have to fill this within transaction scope or VS goes mad lScope.Complete(); } SystemWideLogging.LogServiceClient.LogEvent("Bank :: Bank.Business\\Bank.Business.Components\\TransferProvider.cs :: public void Transfer(double pAmount, int pFromAcctNumber, int pToAcctNumber, String pDescription)", lLogMessage); } catch (Exception lException) { SystemWideLogging.LogServiceClient.LogEvent("Bank :: Bank.Business\\Bank.Business.Components\\TransferProvider.cs :: public void Transfer(double pAmount, int pFromAcctNumber, int pToAcctNumber, String pDescription)", "Transfer Request Failed - received via MSMQ - (pAmount=" + pAmount + " , pFromAcctNumber=" + pFromAcctNumber + " , pToAcctNumber=" + pToAcctNumber + " , pDescription=" + pDescription + " , lMessage= " + lMessage + " )"); Console.WriteLine("Error occured while transferring money: " + lException.Message); lMessage = lException.Message; lOutcome = false; throw; } finally { try { TransferNotificationService.TransferNotificationServiceClient lClient = new TransferNotificationService.TransferNotificationServiceClient(); lClient.NotifyTransferOutcome(lOutcome, lMessage, pDescription); } catch (Exception lException) { SystemWideLogging.LogServiceClient.LogEvent("Bank :: Bank.Business\\Bank.Business.Components\\TransferProvider.cs :: public void Transfer(double pAmount, int pFromAcctNumber, int pToAcctNumber, String pDescription)", "Error occured when sending Notification of Transfer Outcome to VideoStore - via MSMQ - (lException.Message=" + lException.Message + ")"); } } }
public void Transfer(double pAmount, int pFromAcctNumber, int pToAcctNumber, string pOrderNumber) { using (TransactionScope lScope = new TransactionScope()) using (BankEntityModelContainer lContainer = new BankEntityModelContainer()) { try { Account lFromAcct = GetAccountFromNumber(pFromAcctNumber); Account lToAcct = GetAccountFromNumber(pToAcctNumber); if (lFromAcct.Withdraw(pAmount)) { lToAcct.Deposit(pAmount); lContainer.Attach(lFromAcct); lContainer.Attach(lToAcct); lContainer.ObjectStateManager.ChangeObjectState(lFromAcct, System.Data.EntityState.Modified); lContainer.ObjectStateManager.ChangeObjectState(lToAcct, System.Data.EntityState.Modified); lContainer.SaveChanges(); Console.WriteLine("Transfered sucessfully! This payment cost : " + pAmount); TransferNotificationService.ITransferNotificationService lClient = new TransferNotificationService.TransferNotificationServiceClient(); lClient.NotifyTransferResult(true, "Transfer successful! The amount is " + pAmount, pOrderNumber); } else { Console.WriteLine("Transfered failed!"); TransferNotificationService.ITransferNotificationService lClient = new TransferNotificationService.TransferNotificationServiceClient(); lClient.NotifyTransferResult(false, "Transfer failed! Please check your bank account balance!", pOrderNumber); } lScope.Complete(); } catch (Exception lException) { Console.WriteLine("Error occured while transferring money: " + lException.Message); throw; } finally { lScope.Dispose(); } } }