public void Transfer(decimal pAmount, int pFromAcctNumber, int pToAcctNumber, String pDescription) { bool lOutcome = true; String lMessage = "TransferSuccessful"; 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(); lScope.Complete(); } } catch (Exception lException) { Console.WriteLine("Error occured while transferring money: " + lException.Message); lMessage = lException.Message; lOutcome = false; throw; } finally { //here you should know if the outcome of the transfer was successful or not } }
/** * This function transfer "pAmount" from "pFromAcct" to "pToAcct" */ public void Transfer(decimal pAmount, int pFromAcctNumber, int pToAcctNumber, String pDescription, String pReturnAddress) { OperationOutcome.OperationOutcomeResult lResult = OperationOutcome.OperationOutcomeResult.Successful; String lMessage = "TransferSuccessful"; 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(); lScope.Complete(); } } catch (Exception lException) { Console.WriteLine("Error occured while transferring money: " + lException.Message); lMessage = lException.Message; lResult = OperationOutcome.OperationOutcomeResult.Failure; //throw; } finally { /** * notify if the transfer is successful */ IOperationOutcomeService lService = OperationOutcomeServiceFactory.GetOperationOutcomeService(pReturnAddress); //Console.WriteLine("GUID - Bank: " + pDescription); // USE THE RESPONSE QUEUE lService.NotifyOperationOutcome(new OperationOutcome() { Message = lMessage, Outcome = lResult, OperationReference = pDescription }); //here you should know if the outcome of the transfer was successful or not } }
public void Transfer(double pAmount, int pFromAcctNumber, int pToAcctNumber, Guid pOrderNumber, string pReturnAddress) { using (TransactionScope lScope = new TransactionScope()) using (BankEntityModelContainer lContainer = new BankEntityModelContainer()) { //IOperationOutcomeService lOutcomeService = OperationOutcomeServiceFactory.GetOperationOutcomeService(pResultReturnAddress); try { 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); NotificationService.NotificationServiceClient IClient = new NotificationService.NotificationServiceClient("NetMsmqBinding_INotificationService", pReturnAddress); IClient.NotifyBankTransactionCompleted(pOrderNumber, OperationOutcome.Successful); lContainer.SaveChanges(); lScope.Complete(); //lOutcomeService.NotifyOperationOutcome(new OperationOutcome() { Outcome = OperationOutcome.OperationOutcomeResult.Successful }); } catch (Exception lException) { Console.WriteLine("Error occured while transferring money: " + lException.Message); NotificationService.NotificationServiceClient IClient = new NotificationService.NotificationServiceClient("NetMsmqBinding_INotificationService", pReturnAddress); IClient.NotifyBankTransactionCompleted(pOrderNumber, OperationOutcome.Failure); lScope.Complete(); //throw; //lOutcomeService.NotifyOperationOutcome(new OperationOutcome() { Outcome = OperationOutcome.OperationOutcomeResult.Failure, Message = lException.Message }); } } }