/// <summary>
 /// Intializes an instance of <see cref="TransactionViewModel"/>.
 /// </summary>
 /// <param name="entity"></param>
 public TransactionViewModel(Transaction entity)
 {
     if (null == entity)
         throw new ArgumentNullException("entity");
     Entity = entity;
     _amount = Convert.ToString(entity.Amount);
 }
 /// <summary>
 /// Saves a <see cref="Transaction"/> model in persistence database.
 /// </summary>
 /// <param name="transaction">The transaction tha needs to be saved to the persistent DB.</param>
 void ITransactionRepository.SaveTransaction(Transaction transaction)
 {
     SaveTransaction(transaction);
 }
 /// <summary>
 /// Saves a <see cref="Transaction"/> model in persistence database asynchronously.
 /// <remarks>
 /// If this method is consumed, make sure to hook to the <seealso cref="ITransactionRepository.SaveTransactionCompleted"/> event of this instance of <see cref="ITransactionRepository"/>.
 /// </remarks>
 /// </summary>
 /// <param name="transaction">The <see cref="Transaction"/> that needs to be saved in the persistence database.</param>
 void ITransactionRepository.SaveTransactionAsync(Transaction transaction)
 {
     QueueAsyncTask(() =>
     {
         Exception exception = null;
         string message = null;
         try
         {
             //This treachery is for faking a web service call.
             Thread.Sleep(2000);
             SaveTransaction(transaction);
             LogUtil.LogInfo("TransactionRepository",
                             "ITransactionRepository.SaveTransactionAsync",
                             string.Format("Successfully saved the trans: {0}.", transaction.TransactionId));
         }
         catch (Exception ex)
         {
             LogUtil.LogError("TransactionRepository",
                              "ITransactionRepository.SaveTransactionAsync", ex);
             exception = ex;
             message = ErrorMessages.ERR_FAILED_TO_SAVE_TRANS;
         }
         finally
         {
             if (null != _saveTransactionCompleted)
                 _saveTransactionCompleted(this,
                                           new RepositoryTaskFinishedEventArgs(exception, message));
         }
     });
 }
 /// <summary>
 /// Deletes transactions from the persistent database.
 /// </summary>
 /// <param name="transactions">The array of all <see cref="Transaction"/> instances that need to
 ///  be deleted from the persistence database.</param>
 private void DeleteTransactions(Transaction[] transactions)
 {
     if (transactions == null)
         throw new ArgumentNullException("transactions");
     using (var session = SessionProvider.SessionFactory.OpenSession())
     {
         using (var trans = session.BeginTransaction())
         {
             transactions.ForEach(transaction => session.Delete(transaction));
             trans.Commit();
         }
     }
 }
        /// <summary>
        /// Saves a <see cref="Transaction"/> model in persistence database.
        /// </summary>
        /// <param name="transaction">The transaction tha needs to be saved to the persistent DB.</param>
        private void SaveTransaction(Transaction transaction)
        {
            if (transaction == null)
                throw new ArgumentNullException("transaction");

            using (var session = SessionProvider.SessionFactory.OpenSession())
            {
                using (var trans = session.BeginTransaction())
                {
                    session.SaveOrUpdate(transaction);
                    trans.Commit();
                }
            }
        }
 /// <summary>
 /// Gets a new <see cref="Transaction"/> instance.
 /// </summary>
 /// <returns></returns>
 Transaction ITransactionRepository.GetNewTransaction()
 {
     var transaction = new Transaction {TransactionDate = DateTime.Now.Date};
     return transaction;
 }
        /// <summary>
        /// Deletes transactions from the persistent database asynchronously.
        /// <remarks>
        /// If this method is consumed, make sure to hook to the <seealso cref="ITransactionRepository.DeleteTransactionsCompleted"/> event of this instance of <see cref="ITransactionRepository"/>.
        /// </remarks>
        /// </summary>
        /// <param name="transactions">The array of all <see cref="Transaction"/> instances that need to
        ///  be deleted from the persistence database.</param>
        void ITransactionRepository.DeleteTransactionsAsync(Transaction[] transactions)
        {
            QueueAsyncTask(() =>
            {
                Exception exception = null;
                string message = null;
                IList<long> transIds=null;
                try
                {
                    //This treachery is for faking a web service call.
                    Thread.Sleep(2000);
                    DeleteTransactions(transactions);
                    LogUtil.LogInfo("TransactionRepository",
                                    "ITransactionRepository.DeleteTransactionsAsync",
                                    "Successfully deleted the trans.");
                    transIds=transactions.Select(t => t.TransactionId).ToList();

                }
                catch (Exception ex)
                {
                    LogUtil.LogError("TransactionRepository",
                                     "ITransactionRepository.DeleteTransactionsAsync", ex);
                    exception = ex;
                    message = ErrorMessages.ERR_FAILED_TO_DELETE_TRANS;
                }
                finally
                {
                    if (null != _deleteTransactionsCompleted)
                        _deleteTransactionsCompleted(this, new DeleteTransactionsFinishedEventArg(exception, message, transIds));
                }
            });
        }
 /// <summary>
 /// Deletes transactions from the persistent database.
 /// </summary>
 /// <param name="transactions">The array of all <see cref="Transaction"/> instances that need to
 ///  be deleted from the persistence database.</param>
 void ITransactionRepository.DeleteTransactions(Transaction[] transactions)
 {
     DeleteTransactions(transactions);
 }