Tuesday, May 15, 2007

Dataset Vs Datareader - Again!

On reading my previous post on Dataset vs Datareader, one asked me, which one is better. I wanted to reply that. Then I realized there is enough to say that I can put a separate blog entry.

It is a difficult black and white recommendation to make.

Typically, when we design application, we try to achieve something called decoupled layers. Which means different application layer know nothing or minimal about each other. So, we always prefer a disconnected architecture. So, we always try to avoid tying up/carrying around the database connection across different layer. So, dataset kind of object sounds a favorable solution.

So let us think about two simple operations we do. Handling list of rows and handling single row.

One more drawback of Dataset other being bulky, it is not strongly typed. As an attempt to solve this Microsoft came up with strongly typed dataset. But it became too bulky. Then, it made sense to fill a simple object (object with simple getter setter) - Service layer or Business layer would fill in this object using datareader and pass around the Simple object to the "View" layer. Hey, this is when you handling single row. But when it comes to multiple rows, 90% of time we bind them against some.

When it comes to list of objects, intention can be a lot as well - could be that we want to list those records, or we want to enable multiple edit or batch update, or we want to keep the database kind of parent child relationship and transverse through them or we want to keep the database constraints and want to modify the dataset, or we want to integrate with legacy system where XML based object would make sense, or we want to load XML and build a familiar dataset object, or get XML from the underlying datalayer,... etc. Yes, it is really overwhelming to write them together what we can do with Dataset. But usual, worry about Dataset is it being bulk. But MS has something called DiffGram to alleviate the situation to reasonable extent.

So you make decision with design, performance and necessity in mind and try to strike a balance.


varun krishnan said...

Thanks Praveen for the post.

I think the relational info you can maintain in dataset in cool.

I also tried exposing a dataset as a webservice method , it works but there some extra xml attributes in the xml apart from my xml