Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 20-8 extracted from chapter Transactions


Listing 20-7<     > Listing 20-9


This listing can be compiled with the command line:
csc.exe /out:TxList.dll /target:library Example_20_8_to_rename_TxList.cs /r:System.Transactions.dll
Errors: 0 Warnings: 0


Example_20_8_to_rename_TxList.cs
using System;
using System.Collections.Generic;
using System.Transactions;
public class TxList<T> : List<T>, ISinglePhaseNotification {
   private Transaction m_Tx;
   private List<T> dataToCommit;
   private string m_Name;

   public TxList(string name) {
      m_Name = name;
      dataToCommit = new List<T>();
   }
   public void TxAdd(T t) {
      Console.WriteLine(m_Name + ".TxAdd(" + t.ToString() +")");
      if ( m_Tx == null ) 
         Enlist();
      dataToCommit.Add(t);
   }
   private void OnCommit() {
      Console.WriteLine("   "+m_Name+".OnCommit()");
      foreach (T t in dataToCommit)
         base.Add(t);
      dataToCommit.Clear();
   }
   private void OnRollback() {
      dataToCommit.Clear();
   }
   private void Enlist() {
      m_Tx = Transaction.Current;
      if (m_Tx != null) {
         Console.WriteLine("   " + m_Name + ".EnlistVolatile()");
         m_Tx.EnlistVolatile(thisEnlistmentOptions.None);
      }
   }
   public void DisplayContent() {
      Console.Write("--> Content of " + m_Name + " : ");
      foreach (T t in this)
         Console.Write( t.ToString() + ";");
      Console.Write("   dataToCommit: " );
      foreach (T t in dataToCommit)
         Console.Write(t.ToString() + ";");
      Console.WriteLine();
   }

#region IEnlistmentNotification Members
   public void Prepare(PreparingEnlistment preparingEnlistment) {
      Console.WriteLine(m_Name + ".Prepare()");
      preparingEnlistment.Prepared();
   }
   public void Commit(Enlistment enlistment) {
      Console.WriteLine(m_Name + ".Commit()");
      OnCommit();
      enlistment.Done();
   }
   public void InDoubt(Enlistment enlistment) {
      Console.WriteLine(m_Name + ".InDoubt()");
      throw new NotImplementedException();
   }
   public void Rollback(Enlistment enlistment) {
      Console.WriteLine(m_Name + ".Rollback()");
      OnRollback();
      enlistment.Done();
   }
#endregion

#region ISinglePhaseNotification Members
   public void SinglePhaseCommit(
                          SinglePhaseEnlistment singlePhaseEnlistment) {
      Console.WriteLine(m_Name + ".SinglePhaseCommit()");
      OnCommit();
      singlePhaseEnlistment.Committed();
   }
#endregion
}	
Copyright Patrick Smacchia 2006 2007