Friday, November 17, 2006

To DataSet Or Not To DataSet

The DataSet object is nice. really. it has many features.
But I Hate It.

Why DataSet Is BAD
1. Separation Of Concerns: Using the DataSet makes your business or even the ui tier strongly bound to the dataAcccess, because a change in the order or the name of one field in the query will break the data structure of the DataSet.

2. DataSet Is NOT a part of your Domain Model - In order for the DataSet to be generic enough to contain every possible query result, it was build according to the lowest common denominator - which is the structure of a query result - a table.
Because of that, after building the DataSet I need to map it's table-data to the real structure it represents - for example a PersonDM object, which means I'm basically reading the data twice - once from the DB into the DataSet and again from the DataSet into my PersonDM.

3. Too Much Data: The dataset contains useful data - besides the raw query data, for example - db constraints etc., but mostly this data is NOT used! unused data = bad data.

4. Performance - Although it has been greatly improved in 2.0, the DataSet object still has many wonderful features and hence is still pretty "heavy".

So Should I Never Use DataSet???
No, there are many times when using a dataset is a good choice (or even the only choice).

When working with Databases, there are 2 "mode's" of work: Connected & Disconnected.

While disconnected, the use of dataSet is not only recommended but a must!
Think of a client-server application that in order to show a simple grid it needs to connect to the remote DB Server and query it.
In this case, working with dataSet will greatly improve your performance - instead of running row-by-row like the DataReader, you query the database only once receiving bulk data into the DataSet.

The problem is, that Web applications - for that matter (only),belong to the connected environment category - because our client is the WebServer and not the browser.

Conclusion:
The DataSet is a very rich object, so much rich that mostly it's an overhead.
Before choosing whether to work with DataSet or DataReader, check first whether your application is connected or disconnected.

Just my 2 penny's on DataSet ;)

No comments: