For Programmers: Free Programming Magazines  


Home > Archive > C# > June 2004 > BaseBusiness









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author BaseBusiness
Kal

2004-06-03, 7:31 pm

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using SpeedDataAccess;

namespace SPEEDBusiness
{
/// <summary>
/// Summary description for BaseBusiness.
/// </summary>
public abstract class BaseBusiness
{


private bool m_bIsInTransaction = false;
private SqlConnection m_cn;
private SqlTransaction m_txn;
private bool bStartedTransaction = false;

public BaseBusiness(){}

//--------------------------------------------------
//This constructor is used when the business object is
//participating in a database transaction;
//--------------------------------------------------
public BaseBusiness(SqlConnection cn, SqlTransaction txn)
{
m_cn = cn;
Transaction = txn;

if(txn != null)
{
m_bIsInTransaction = true;
}
}

//---------------------------------------------------
//Returns a boolean indicating whether or not this
//object is currently participating in a transaction.
//----------------------------------------------------
public bool IsInTransaction()
{
return m_bIsInTransaction;
}

//----------------------------------------------------
//Responsible for creating and opening a database
//connection. If the object is part of a transaction
//the database connection passed during the constructor
//is returned.
//-----------------------------------------------------
protected SqlConnection OpenDbConnection(string sCnString)
{
if (IsInTransaction())
{
return m_cn;
}
else
{
m_cn = new SqlConnection(sCnString);
m_cn.Open();
return m_cn;
}
}

//------------------------------------------------------------
//Used to close the database connection used by this class.
//A special transactional check is done so the connection is
//not closed if this object is participating in a transaction.
//------------------------------------------------------------
protected void CloseDbConnection()
{
if (!IsInTransaction())
{
if (m_cn != null)
{
m_cn.Close();
}
}
}

//----------------------------------------------------
//Returns a reference to the internal connection object
//used by this class.
//-----------------------------------------------------
protected SqlConnection Connection
{
get
{
if (m_cn == null)
{
m_cn = new SqlConnection();
}
return m_cn;
}
}

//----------------------------------------------------------
//Allows the interal Transaction object of this class to be set
//and returned.
//-------------------------------------------------------------
protected SqlTransaction Transaction
{
get
{
return m_txn;
}
set
{
m_txn = value;
}
}

protected void StartTransaction()
{
if (!IsInTransaction())
{
OpenDbConnection(SpeedUDL.SPEED_CONNECT);
Transaction = Connection.BeginTransaction();
bStartedTransaction = true;
}
}

public bool IsTransactionCreator
{
get
{
return bStartedTransaction;
}
}

}
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com