guyS's WebLog

IShare, My DotNet Fingerprint

IDispose implementation for wrapping a Transaction instance

This post include IDispose implementation for wrapping a Transaction instance (same datasource). On this snippet I'm using entLib for creating my Transaction instance. I also attached the DAL caller code snipped and the DAL method that is using the Transaction instance. Its very important to use Dispose implementation patterns whenever we want to ensure that our clean out mechanism is called and we are releasing any expensive resources like a Connection and/or Transaction instance/s.    

Code Snipped 1: Wrapping the Transaction in a class

  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Diagnostics;
  5 using MyCompany.MyDepartment.TheProject.Common;
  6
  7 using Microsoft.Practices.EnterpriseLibrary.Configuration;  
  8 using Microsoft.Practices.EnterpriseLibrary.Data; 
  9
 10 namespace MyCompany.MyDepartment.TheProject.DAL
 11 {
 12  /// <summary>
 13  /// Summary description for DBTransaction.
 14  /// </summary>
 15  public class DBTransaction: IDisposable
 16  {
 17   #region private members
 18   private bool disposed=false;
 19   private IDbConnection m_DBConnection;
 20   private IDbTransaction m_DBTransaction;
 21   private static DBTransaction m_Transaction;
 22   #endregion
 23   #region ctor
 24   protected DBTransaction()
 25   {
 26    Database db = DatabaseFactory.CreateDatabase( AppConstant.ConfigDataInstanceKey);
 27    m_DBConnection = db.GetConnection();
 28    m_DBConnection.Open();
 29    m_DBTransaction = m_DBConnection.BeginTransaction();
 30   }
 31   #endregion
 32   #region transaction services
 33   public void Commit()
 34   {
 35    if (m_Transaction==null)
 36     return;
 37
 38    m_Transaction.m_DBTransaction.Commit();
 39   }
 40   public void RollBack()
 41   {
 42    if (m_Transaction==null)
 43     return;
 44
 45    m_Transaction.m_DBTransaction.Rollback();
 46   }
 47   #endregion
 48   #region Transaction instance
 49   public static DBTransaction CreateTransaction()
 50   {
 51    if (m_Transaction==null)
 52     m_Transaction = new DBTransaction();
 53    return m_Transaction;
 54   }
 55   public static IDbTransaction Transaction
 56   {
 57    get
 58    {
 59     if (m_Transaction==null)
 60      return null;
 61
 62     return m_Transaction.m_DBTransaction;
 63    }
 64   }
 65   #endregion
 66   #region IDispose impl
 67   ~DBTransaction()
 68   {
 69    Trace.WriteLine("ClassBeingTested: Destructor");
 70    // call Dispose with false.  Since we're in the
 71    // destructor call, the managed resources will be
 72    // disposed of anyways.
 73    Dispose(false);
 74   }
 75   public void Dispose(bool doDispose)
 76   {
 77    if (!this.disposed)
 78    {
 79     Trace.WriteLine("ClassBeingTested: Resources not disposed");
 80     if (doDispose)
 81     {
 82      Trace.WriteLine("ClassBeingTested: Disposing managed resources");
 83      // dispose managed resources
 84      if (m_DBConnection != null)
 85      {
 86       m_DBConnection.Dispose();
 87       m_DBTransaction.Dispose();
 88       m_DBConnection=null;
 89       m_DBTransaction=null;
 90      }
 91     }
 92     // dispose unmanaged resources
 93     Trace.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
 94     disposed=true;
 95    }
 96    else
 97    {
 98     Trace.WriteLine("ClassBeingTested: Resources already disposed");
 99    }
100   }
101   #region IDisposable Members
102   public void Dispose()
103   {
104    Trace.WriteLine("ClassBeingTested: Dispose");
105    // dispose of the managed and unmanaged resources
106    Dispose(true);
107
108    // tell the GC that the Finalize process no longer needs
109    // to be run for this object.
110    GC.SuppressFinalize(this);
111   }
112   #endregion
113   #endregion
114  }
115 }
116
Code Snipped 2: Using it from a DAL method

  1 protected static void UpdateDataSet(Database db, DataSet data, string tableName,
  2    DBCommandWrapper insertCommand, DBCommandWrapper updateCommand, DBCommandWrapper deleteCommand,
  3    UpdateBehavior updateBehaviour)
  4   {
  5    try
  6    {
  7     if (DBTransaction.Transaction==null)
  8      db.UpdateDataSet(data, TnCase.tName, insertCommand, updateCommand, deleteCommand,
  9       updateBehaviour);
 10     else
 11      db.UpdateDataSet(data, TnCase.tName, insertCommand, updateCommand, deleteCommand,
 12       DBTransaction.Transaction);
 13
 14     if (DBTransaction.Transaction!=null)
 15      DBTransaction.Transaction.Commit();
 16     
 17    }
 18    catch (Exception ex)
 19    {
 20     if (DBTransaction.Transaction!=null)
 21      DBTransaction.Transaction.Rollback();
 22     //do logging
 23     ExceptionHelper.HandleException(ex, ExceptionPolicies.DALPolicy);    
 24    }
 25   }
Code Snipped 3: Wrapping the Transaction in a class

  1 public void TestTransactionWithAddDelete()
  2   {
  3    using (DBTransaction transaction = DBTransaction.CreateTransaction())
  4    {
  5     try
  6     {
  7      //call the DAL Save method goes here
  8      transaction.Commit();
  9     }
 10     catch(Exception ex)
 11     {
 12      Trace.WriteLine(ex);
 13      transaction.RollBack();
 14     }
 15    }
 16   }
Happy Coding:-)

Comments

TrackBack said:

# June 17, 2005 1:11 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)