Good DataSet, Bad DataSet

I recently had to work on an older project that used DataSets for almost all it’s in memory data. The code used to look like this:

public void Page_Load(object Sender, EventArgs e)
{
    SomeWebService service = new SomeWebService();
    DataSet data = service.GetData(userId);
    grid.DataSource = data;
    grid.DataBind();
}

I needed to change it to call the service multiple times, combine all the data, and sort it:

public void Page_Load(object Sender, EventArgs e)
{
    SomeWebService service = new SomeWebService();
    DataSet data = new DataSet();
    foreach(int userId in userIds)
        data.Merge(service.GetData());
    grid.DefaultView.Sort = "date";
    grid.DataSource = data;
    grid.DataBind();
}

Surprisingly this actually worked. DataSets worked really well here because it was a reporting application with lots of tabular data that used webservices and datagrids. This is what DataSets were designed for.

I have seen many other times where a DataSet is used instead of a collection of objects and the result is just ugly. Sometimes it ends up like this:

public void Page_Load(object Sender, EventArgs e)
{
    SomeWebService service = new SomeWebService();
    DataSet data = service.GetData();
    if(UserUtility.CheckPassword(data, userId, password.Text))
        error.Text = "Invalid password for user " + data.Tables[0].Select("userId = " + userId)[0]["userName"]
}

Code dealing with users can’t go in the User class because it doesn’t exist, so a UserUtility class with all static methods is created. The code often makes a lot of assumptions: the number of tables, the names of fields, etc. These assumptions could easily be broken. the code is very fragile. If there was a User class in this case you would get better compile time errors, the code would be cleaner and better organized, and would probably be more reusable.

DataSets have their place and they work well, just learn to recognize when a DataSet is the wrong solution to a problem. It seems like a lot of technologies in the .net world are pushed way past what they were designed to do and I’m not sure why that is. Is Microsoft to blame? The developers? The platform? Marketing? Who knows.

If all you have is a hammer…

Posted in Software Development and tagged . Permalink.

5 Responses to Good DataSet, Bad DataSet

  1. Pingback: Ayende @ Rahien

  2. It surprises me to see untyped datasets unless you have no idea what the data is going to look like.

    You should look into using typed datasets in your sample

    S
  3. In the actual code I was working with yesterday it was a typed dataset. I left it out of the example to make it shorter. Though, I’m not sure a typed dataset did anymore than an untyped in this case.

    Untyped seems more common than typed in the kind of code where there are a bunch of fragile “helper” classes working on datasets. I never really noticed this before. Maybe some of those projects could have been improved by switching to typed datasets.

    Anonymous
  4. Using datasets between services also goes against SOA, since datasets use data type of ‘Any’, and generates an obscene amount of XML wrapping the actual data.

    Adi
  5. Pingback: vorpal.cc/blog » DataSet Followup: Typed DataSets

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>