J
jehugaleahsa
Hello:
Quick design question: should a derived class be expected to provide a
custom exception to the super class?
We caught ourselves writing the same code for executing SQL; something
like this:
using (IDbCommand command = _connection.CreateCommand())
{
command.CommandText = "";
using (IDataReader reader = command.ExecuteReader())
{
List<MyDO> results = new List<MyDO>();
while (reader.Read())
{
MyDo result = getResult(reader);
results.Add(result);
}
return results;
}
}
This is a lot of code to duplicate over and over in the application.
We created a simple class similar to the following to do most of this
for us:
public abstract class Command<TDataObject>
{
private readonly IDbConnection _connection;
protected Command(IDbConnection connection)
{
_connection = connection;
}
protected abstract string CommandText { get; }
protected abstract void AddParameters(<another helper class for
adding parameters>);
protected abstract TDataObject GetResult(IDataRecord record);
public IEnumerable<TDataObject> GetResults()
{
try
{
return getResults();
}
catch (DbException exception)
{ // throw custom exception
}
}
private IEnumerable<TDataObject> getResults()
{
using (IDbCommand command = _connection.CreateCommand())
{
command.CommandText = GetCommandText();
AddParameters(<our customer class for retrieving
parameters>);
using (IDataReader reader = command.ExecuteReader())
{
List<TDataObject> results = new List<TDataObject>();
while (reader.Read())
{
TDataObject result = GetResult(reader);
results.Add(result);
}
return results;
}
}
}
}
When a DbException occurs, we would like to wrap it in a custom
exception. The specific exception depends on the concrete class.
Should I add another method like this:
protected abstract Exception GetCustomException(DbException
exception);
This way the concrete class can wrap the given exception however is
appropriate. It seems a little weird to delegate exception handling to
the derived class. What are your thoughts?
Quick design question: should a derived class be expected to provide a
custom exception to the super class?
We caught ourselves writing the same code for executing SQL; something
like this:
using (IDbCommand command = _connection.CreateCommand())
{
command.CommandText = "";
using (IDataReader reader = command.ExecuteReader())
{
List<MyDO> results = new List<MyDO>();
while (reader.Read())
{
MyDo result = getResult(reader);
results.Add(result);
}
return results;
}
}
This is a lot of code to duplicate over and over in the application.
We created a simple class similar to the following to do most of this
for us:
public abstract class Command<TDataObject>
{
private readonly IDbConnection _connection;
protected Command(IDbConnection connection)
{
_connection = connection;
}
protected abstract string CommandText { get; }
protected abstract void AddParameters(<another helper class for
adding parameters>);
protected abstract TDataObject GetResult(IDataRecord record);
public IEnumerable<TDataObject> GetResults()
{
try
{
return getResults();
}
catch (DbException exception)
{ // throw custom exception
}
}
private IEnumerable<TDataObject> getResults()
{
using (IDbCommand command = _connection.CreateCommand())
{
command.CommandText = GetCommandText();
AddParameters(<our customer class for retrieving
parameters>);
using (IDataReader reader = command.ExecuteReader())
{
List<TDataObject> results = new List<TDataObject>();
while (reader.Read())
{
TDataObject result = GetResult(reader);
results.Add(result);
}
return results;
}
}
}
}
When a DbException occurs, we would like to wrap it in a custom
exception. The specific exception depends on the concrete class.
Should I add another method like this:
protected abstract Exception GetCustomException(DbException
exception);
This way the concrete class can wrap the given exception however is
appropriate. It seems a little weird to delegate exception handling to
the derived class. What are your thoughts?