Nerseus
Danner
We have a small debate at work and I'm wondering if there's any information on going one way or the other on this.
The setup is that there exists a database utility class - a class that serves only to expose some static methods. It looks something like this:
So the general syntax for using the static method is:
DataAccess.ExecuteNonQuery("procName 'hello world'");
Now the argument comes up over whether a specific Data Access Layer class should inherit from this class or not. Here's a sample that does inherit:
Again, ignore the syntax of passing params to a stored proc as a string without any SQL Injection checks.
In the above, I'd rather NOT have CustomerDataAccess inherit from DataAccess. The only change is that the call to ExecuteNonQuery changes to:
But others argue that by inheriting from DataAccess, you're declaring your intentions that this is a DataAccess component and the method calls "look" like they're part of CustomerDataAccess.
I'd argue that they're NOT part of CustomerDataAccess but that they're still part of DataAccess since they're static.
I guess I don't see the benefit of inheritance in this case a both classes are really utility classes.
Any thoughts?
-nerseus
The setup is that there exists a database utility class - a class that serves only to expose some static methods. It looks something like this:
C#:
public class DataAccess
{
protected static readonly string connectionString;
static DataAccess()
{
connectionString = ""; // Code ommitted - assumes it gets the connection string
}
internal static DataSet ExecuteNonQuery(string commandText)
{
// Code to create a DB command object, set the connection string
// and call ExecuteNonQuery
// Also handles exception handling by logging errors and other misc details
}
}
So the general syntax for using the static method is:
DataAccess.ExecuteNonQuery("procName 'hello world'");
Now the argument comes up over whether a specific Data Access Layer class should inherit from this class or not. Here's a sample that does inherit:
C#:
internal sealed class CustomerDataAccess : DataAccess
{
// Private constructor so it can't be created - in 2005 you'd declare the
// class as static
private CustomerDataAccess() {}
public static void SaveCustomer(DataSet ds)
{
ExecuteNonQuery("UpdateCust '" + ds.GetXml() + "'");
}
}
Again, ignore the syntax of passing params to a stored proc as a string without any SQL Injection checks.
In the above, I'd rather NOT have CustomerDataAccess inherit from DataAccess. The only change is that the call to ExecuteNonQuery changes to:
C#:
public static void SaveCustomer(DataSet ds)
{
DataAccess.ExecuteNonQuery("UpdateCust '" + ds.GetXml() + "'");
}
But others argue that by inheriting from DataAccess, you're declaring your intentions that this is a DataAccess component and the method calls "look" like they're part of CustomerDataAccess.
I'd argue that they're NOT part of CustomerDataAccess but that they're still part of DataAccess since they're static.
I guess I don't see the benefit of inheritance in this case a both classes are really utility classes.
Any thoughts?
-nerseus