CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

Scott doesn't use datasets in ASP .NET Applications

Scott doesn't.  I do... sometimes.

I generally prefer business objects and collections thereof for a good chunk of my ASP .NET applications.  This is mostly because by extending business objects with properties, methods, and events I can gain a level of control that isn't possible with the dataset.  Using a datareader to populate these objects is fast and easy.

However... in the case where I am just displaying data, such as binding results to a combo box or showing a summary grid, I tend to use a datatable or dataset.  But why?

  1. I don't want an open connection/datareader in my UI layer.  I believe in separation of code into logical layers which means that binding a datagrid directly to a datareader is a no-no.  Fetching a datatable/dataset from the database layer is fine though.
  2. There is a reduction in the amount of code you have to write to populate and kick back a datatable rather than a dataset.  In fact, if you are calling a stored procedure, you can even tweak the fields passed back without editing the code in the datalayer, the datatable will populate all the results automatically.  In the business object/collection world you'll have to tweak the code in the stored procedure, the data layer, and then the UI.  With datatables you can eliminate a step.
  3. Aggregates, joins, and sorting!  Much MUCH faster and more efficient to let SQL Server do this than to force your webserver to do this walking collections, etc.  Not to mention the added complexity of handling collections in collections in collections to represent joins in a business object.  Congratulations, you just added a ton of code complexity to determine when to populate the object and its joins or just populate the object.  People that use lazy-loading without thinking end up with hundreds of database calls for a single grid bind when they refer to properties that require a lazy load in the grid.
  4. Is populating a collection of objects really that much more performant that populating a datatable?  I doubt it... either way you're walking data and inserting records into an object.

So there you have it.  I'm not against business objects/collections.  I freaking love them... however, if you're just displaying data, not using any business logic, then it is my opinion that you are far better off passing out a datatable/dataset than trying to do SQL Server tasks inside your business objects.

Perhaps Sahil or John Papa can supply statistics for #4 and #5.  I have little patience for such measurements.  =)

Update: Here's a link to some datareader vs datatable stats: http://www.devx.com/vb2themax/article/19887/1954

Shows that the datareader is far more performant as you add more and more data.  One caveat here before people glance and freak out at the difference: As a commented above, I use the datatable to show summary, join, you know... special data.  The article shows how performance degrades all the way up to 10,000 records... realistically you should never be displaying a client more than 100-200 over the web if not for performance purposes but just readability.  It's just too much information for the average human to process easily.  If you feel like you have to show 10,000 records I suggest an alternative solution involving lots of filters.


Published May 02 2005, 07:44 PM by Eric Wise
Filed under: ,

Comments

.Net Adventures said:

# May 2, 2005 11:54 PM

Geoff Appleby said:

Dude.

It's good to see someone who thinks like I do. If We're wrong, at least we're not alone :)

I'm quite fluent in datareader work...very unfluent in anything else :) I have absolutely no idea how to databind - I just simply haven't needed to do it in my line of work...
# May 3, 2005 3:48 AM

JOEL'S BLOG said:

# May 5, 2005 4:22 PM

Joel's Virtual Desktop said:

# May 5, 2005 4:23 PM

Joel Ross said:

# May 5, 2005 4:23 PM

Scott Mitchell said:

**************************************
You said: "... realistically you should never be displaying a client more than 100-200 over the web if not for performance purposes but just readability ..."
**************************************

I agree, but what can happen sometimes is the following. You have a table that the client says, "This table will have, at most, about 100 records. So you say to yourself, "Ok, I'll show 20 records per page on a DataGrid. Since there will never be more than 100 records, I'll just use default paging (which, as you know, grabs ALL of the records on each page shown)." This works fine enough until your client breaks his "promise" and that table grows to several hundred or thousands of rows. Eep. You'd like to go back and convert the DataGrid to use custom paging, but the project is over; and now the client's customers are a bit pissed, because the page is so slow. Ick.

(Ok, maybe it's a contrived example, but it shows how you can be fetching thousands of records on a Web page, even if you're not displaying them all...)
# May 6, 2005 8:47 AM

Eric Wise said:

A valid point, even if a contrived example. Of course since I always do custom paging returning only the subset of data I'm working with it's not a problem for me personally. =)
# May 6, 2005 9:17 AM

Javier Luna said:

I believe that any DataLayer must be a simple code block, that they allow operations against DB.

That code block would not have to know on the Business Entities. Single to specialize it is to execute the operations (Store Procedures and SQL Sentences) against the engine DB (SQL, Oracle, DB2, etc.), with which this setting.

Finally, I invite to you to download the DataLayer.Primitives Public Version.

This is very cool Data Layer :)

DataLayer.Primitives - Readme!
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1389
# May 12, 2005 11:25 PM

Rickie said:

Scottdoesn'tusedatasetsinASP.NETApplications
# July 23, 2006 10:10 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors