Title Image

Don Xml's Grok This

The home of Don Demsak
Welcome to Don Xml's Grok This Sign in | Help
in Search

This Blog

Syndication

Site Sponsors

DonXml's All Things Techie

Are DataSets the Spawn Of Satan?

Scott Hansleman has been talking about multi-tiered architecture and his latest almost gibes with my view: DataSets have their place, but not as a business object, (especially if you are going to return them from a web service). [Also see Barry Gervin's post Datasets vs. Custom Entities]

Scott had previously posted a good explanation of multi-tiered design.

Here’s a good snippet on the business layer:

Things your BizLogic Layer shouldn't know about: Ideally your business logic layer shouldn't know there is a database.  It shouldn't know about connection strings or SQL.  It shouldn't know about much of anything except whatever format you choose to represent your objects in (preferably NOT DataSets.)

But, at the end of Scott’s post on DataSets, he points to TheServerSide.Net’s best practices, which are very old school, and are about to be phased out of existence.  The one major problem I have with the best practices is that they say DataReaders are better than DataSets.  DataReaders break the rules on multi-tier design (because you can’t close a connection until you are done with a DataReader, you are now forcing Data Access logic in the Business Layer).  

But, you still need some sort of data transport object to expose your data to the business layer.  XmlReader is a very good one, especially if you added XmlSerialization to your BizObjects.  If you are using SQLXML (specifically the For XML clause in SQL Server), XmlReader may even make even more sense, except for the fact that the Sq1Command.ExecuteXmlReader would have to be read into a local memory stream and then pass up the XmlReader for the memory stream (in order to close the connection) to the business layer (basically creating a local cache).  Well, that kind of sucks, and it is only the best option if you are using XmlSerialization (and if there was no business logic involved with the transformation from XML to object, why even de-serialize it.  Just spit it out of the web service, raw). 

So if XmlReader isn’t the (usual) correct answer, what is?  XPathNavigator BABY!  Why, because it can be used to navigate over many different data stores, not just XML.  And with it, comes the power (and the ability to compile and cache queries) of XPath.  Just load a XPathDocument (which is optimized for XPath queries over XML) using the same XmlReader returned from the SqlCommand.ExecuteXmlReader(), and then generate a navigator from the root, XPathDocument.CreateNavigator(), and pass that up to the business layer.  From there, the business layer can create, compiled, and cache XPathExpressions to transform the XML to business objects.   For each business object, create a constructor that accepts a cloned XPathNavigator.  And as a side benefit, XPathDocuments are read only (for now), so you can be sure that a rogue business object doesn’t screw with the data from the DAL.  And what if you aren't using a store that exposes XML (like OLEDB)?  You can always drop back to a DataSet and use System.Xml.XmlDataDocument.CreateNavigator.  Now, your business layer truly doesn't care about the data store.  And if you are really using some obscure data store, you could always add the IXPathNavigable interface, and you are still good to go. 

Oh, I’m sure you would like some working examples.  Well, I’ve been working on them, since I replied to Jay Kimble’s post on DataSets.  I’m almost done, and should have a C# version up within a day or two, utilizing DAAB 3.1 RC1.

Published Tuesday, June 01, 2004 1:35 PM by donxml
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Scott Hanselman said:

Madness! ;) You know that those best practices from TSS.NET were from TechEd 2004 (last week), right?

What's being phased out, and when?
June 1, 2004 4:37 PM

DonXML Demsak said:

Just checkout the recent VS2K5 drop to see what I mean. It is (mostly) all in there. Death to the DOM, and death to the DataSet (well, they are still there, but the writing is on the wall).
June 1, 2004 4:58 PM

TrackBack said:

Soon to be joining futuritis
June 1, 2004 6:47 PM

Darrell said:

Nice post, Don.
June 1, 2004 7:14 PM

TrackBack said:

Datasets vs. Custom Entities
June 2, 2004 12:46 PM

TrackBack said:

Blog Clusters
June 3, 2004 8:37 AM

Bryan Ax said:

What about this approach? Is the xml still better?

private Customer LoadFromReader(IDataReader reader)

{
string name = reader[“name”].ToString();
string phone = reader[“phone”]. ToString();
return new Customer(name, phone);
}


Public Customer GetByGuid(Guid guid)
{
Do db stuff to call a proc and return a reader. Pass the reader to loadfromreader function, return result.
return this.LoadFromReader….
}

Is this bad practice b/c the data layer now “knows” about the business object? Better to just return the xml?


Sincerely,

Bryan Ax
Always learning about n-tier…

June 4, 2004 11:03 AM

R Tate said:

I started using the concept of an IRowConsumer interface which is fed an IDataRecord for each row. The Process(IDataRecord) method returns true if it wants more rows. Then each BusinessObject has one or more associated classes that implement IRowConsumer. Since the data layer opened the connection and created the DataReader, and just does call-backs to spoon-feed a business object builder, we don't quite expose the DataReader to the business layer, although they could cast IDataRecord to SqlClient.DataReader with no guarantee of success. The paranoid among us could create a wrapper class for DataReader that implements only IDataRecord + nextRow().

IANAA (I am not an architect)

Roy Tate
June 21, 2004 1:57 PM

Asbjørn Ulsberg said:

I have to say that I love the idea. But what about two-way data layers? If I want to not only request data, but also submit something back, how the heck am I supposed to do that? Send some sort of reader to the different Save() methods, or what?

A read-only data layer is perfect for XPathNavigator, but what about read-write?
June 23, 2004 1:50 PM

TrackBack said:

June 25, 2004 1:10 PM

TrackBack said:

June 28, 2004 6:16 AM

TrackBack said:

June 28, 2004 6:17 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About donxml

I’m an independent consultant, specializing in .Net solutions architecture, based out of New Jersey who also doubles as an evangelist for XML, Domain Driven Design, enterprise architecture and .Net. I do not work for Microsoft, the W3C or any other big company that you may know of (at least not yet). I’ve been an indie for over ten years, and although I’ve been tempted a couple times to take a job with companies like Microsoft, I’ve haven’t found something better than my current situation. I work mostly with the large pharmaceuticals that are based here in New Jersey, and usually find myself on long term contracts. Definitely not the prototypical indie consultant, but it lets me dedicate time to my non-income generating activities like the developer community stuff, plus financing open source projects like XPathmania and MVP-XML. If you would like to talk to me about doing some contract work, just contact me via the contact page. My rates vary widely, depending on lots of different variables, but mostly distance from Jersey, and type of work. Plus, I’ve been known to donate some of my code for various projects.
Powered by Community Server, by Telligent Systems