Db Class

DbClass is a software AntiPattern.

A developer creates a class that has methods that are passed the name of a stored procedure or a sql statement and an array of parameters. The methods generally return data tables or scalar values or data sets.

Here is an example of a call to such a method:

 DataTable customers = DB.GetTable("dbo.GetCustomers", params);

Where "dbo.GetCustomers" is the name of a stored procedure and "params" is a database parameter array.

What's wrong with this pattern is that the calling code has to know the structure of the database - name of stored procedure, names and types of the parameters. A true Data Access Layer hides the database from the other parts of the application.

I've seen this DbClass AntiPattern used in two ways:

The 1st way exposes the database structure to the harsh light of day - not a good idea. Try to make some database changes or change the parameters of a stored procedure and you will be doing text searches and endless regression testing to make sure you didn't break something.

The 2nd way is not as bad, at least the stored procedure and the parameters are hidden from the UI code but these methods return generic DataTable objects and then the code that made the call has to know the names of the table columns, which brings us back to the same issue as before - code outside the data access layer has to know the database structure.

If you really have to use DataSets and DataTables for passing your data around then use Typed DataSets (.NET) or their equivalent in the language of your choice.

Better still is to use a DomainModel pattern and an ORM tool that converts data from a database into objects and collections of objects. NHibernate, LLBLGen and now Entity Framework are just three of the many choices available.


EditText of this page (last edited August 22, 2010) or FindPage with title or text search