//private static readonly String sPublishQueuePath = ".\\private$\\BankTransferQueueTransacted"; //private static void EnsureMessageQueuesExists() //{ // // Create the transacted MSMQ queue if necessary. // if (!MessageQueue.Exists(sPublishQueuePath)) // MessageQueue.Create(sPublishQueuePath, true); //} private static void CreateDummyEntities() { using (TransactionScope lScope = new TransactionScope()) using (BankEntityModelContainer lContainer = new BankEntityModelContainer()) { if (lContainer.Accounts.Count() == 0) { Customer lVideoStore = new Customer(); Account lVSAccount = new Account() { AccountNumber = 123, Balance = 0 }; lVideoStore.Accounts.Add(lVSAccount); Customer lCustomer = new Customer(); Account lCustAccount = new Account() { AccountNumber = 456, Balance = 20 }; lCustomer.Accounts.Add(lCustAccount); lContainer.Customers.AddObject(lVideoStore); lContainer.Customers.AddObject(lCustomer); lContainer.SaveChanges(); lScope.Complete(); } } }
private Account GetAccountFromNumber(int pToAcctNumber) { using (BankEntityModelContainer lContainer = new BankEntityModelContainer()) { return lContainer.Accounts.Where((pAcct) => (pAcct.AccountNumber == pToAcctNumber)).FirstOrDefault(); } }
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 }); } } }