protected override void HandleTransfer(CAP_CQRSEntities entities, TransferMessage transfer) { Console.WriteLine(String.Format("Increase {0}.", transfer.ToAccount)); AccountBalance to = GetAccountEntity(transfer.ToAccount, entities); to.Balance += transfer.Amount; }
protected override void HandleTransfer(CAP_CQRSEntities entities, TransferMessage transfer) { Console.WriteLine(String.Format("Decrease {0}.", transfer.FromAccount)); AccountBalance from = GetAccountEntity(transfer.FromAccount, entities); from.Balance -= transfer.Amount; }
protected static AccountBalance GetAccountEntity(string account, CAP_CQRSEntities 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; }
protected static AccountBalance GetAccountEntity(string account, CAP_CQRSEntities 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); }
private void ThreadProc() { while (!_stopping.WaitOne(10)) { try { using (CAP_CQRSEntities entities = new CAP_CQRSEntities()) { using (var tx = new TransactionScope()) { using (MessageQueue queue = new MessageQueue(_queueName)) { Message message = queue.Receive(TimeSpan.FromSeconds(1.0), MessageQueueTransactionType.Automatic); _wroteEmptyMessage = false; message.Formatter = Formatter; TransferMessage transfer = (TransferMessage)message.Body; HandleTransfer(entities, transfer); entities.SaveChanges(); tx.Complete(); } } } } catch (MessageQueueException mqex) { if (mqex.ErrorCode != TimeoutErrorCode) { Console.WriteLine(mqex.Message); } else { if (!_wroteEmptyMessage) { Console.WriteLine(String.Format("{0} queue empty.", _threadName)); } _wroteEmptyMessage = true; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
protected abstract void HandleTransfer(CAP_CQRSEntities entities, TransferMessage transfer);
private void ThreadProc() { while (!_stopping.WaitOne(10)) { try { using (CAP_CQRSEntities entities = new CAP_CQRSEntities()) { using (var tx = new TransactionScope()) { using (MessageQueue queue = new MessageQueue(_queueName)) { Message message = queue.Receive(TimeSpan.FromSeconds(1.0), MessageQueueTransactionType.Automatic); _wroteEmptyMessage = false; message.Formatter = Formatter; TransferMessage transfer = (TransferMessage)message.Body; HandleTransfer(entities, transfer); entities.SaveChanges(); tx.Complete(); } } } } catch (MessageQueueException mqex) { if (mqex.ErrorCode != TimeoutErrorCode) Console.WriteLine(mqex.Message); else { if (!_wroteEmptyMessage) Console.WriteLine(String.Format("{0} queue empty.", _threadName)); _wroteEmptyMessage = true; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }