Removing repetivite code for connection, trans, exeception etc.

  • Thread starter Thread starter Tim Smith
  • Start date Start date
T

Tim Smith

I have a lot of code which follows the general pattern below.

Is there an easy (or hard) way of avoiding repeating the same code
over and over. I do need to manage the connection at this level, but
I am worried developers will forget to commit and/or close the
connection. If I could auto wrap around it somehow..?

public void SomeUpdate() {
OracleConnection connection = null;
OracleTransaction trans = null;
DataSet ds = null;
try
{
connection = GetConnection();
trans = connection.BeginTransaction();

//6-10 lines of code

trans.Commit();
connection.Close();
}
catch (Exception e)
{
trans.Rollback();
connection.Close(); LogError(this,e.Message +
Environment.NewLine + ids);
throw new Exception(e.Message + Environment.NewLine +
e.StackTrace.ToString());
}
}
 
Hi Tim...

I think u can use the microsoft application blocks for data. U can create a
wrapper class for that component and thus avoid coding for creating and
destrying the stuff....in each and every ,method

smiles,
Laiju
 
Hi,

all of these .NET Data Provider objects Connections, Commands, DataAdapters
implement a common interfaces viz. IDbConnection, IDbCommand, IDataAdapter.
So write generic implementation codes using these interfaces & call them
accordingly.

Regards
Joyjit
 
Back
Top