Scott Hanselman

Returning DataSets from WebServices is the Spawn of Satan and Represents All That Is Truly Evil in the World

June 1, '04 Comments [11] Posted in TechEd | Web Services | XML
Sponsored By

(Nah, I don't really believe that, but it's a good title, no?  DataSets have there place, just not as publically visible Business Objects or from publically accessible WebServices.)

Barry Gervin commented on my "quick bash at DataSets" and that I didn't explain my reasonining.  In his post, Barry commented on Harry Pierson's statement that one shouldn't use DataSets in a Web Service because they aren't compatible with non .NET Platforms.  Barry says, "This isn't true. A DataSet is just XML."  Well, of course it's XML, but if I say, hey take this sentence, it's in the ASCII character set (who care's if it isn't English) Le "DataSet" n'est pas votre ami si vous faites des Services de Web.  Well, Barry can understand that, but I no hablo French. ;)

DataSets are bowls, not fruit.  Do you really want to return bowls?

A DataSet is an object, right?  But it's not a Domain Object, it's not an "Apple" or "Orange" - it's an object of type "DataSet."  A DataSet is a bowl (one that knows about the backing Data Store).  A DataSet is an object that knows how to HOLD Rows and Columns.  It's an object that knows a LOT about the Database.  But I don't want to return bowls.  I want to return Domain Objects, like "Apples."

"Use Strongly Typed DataSets," you say.  "They are the same as Objects, and look how intellisquish works now!"

No, they still aren't Domain Objects, a Strongly Typed DataSet is just a bowl with a picture of an Apple on it.  "Look there's an Apple INSIDE - we've broken it down into columns!"  DataSets are a shoddy replacement for a good Domain Model (and that includes Strongly Typed DataSets). 

Barry has a very good argument for the use of DataSets on his site, and I won't go through his list agreeing and disagreeing with various points.  I will say this, however, it seems that his arguments support the use of DataSets in a Data Access Layer - not in a Business Object Layer.  THAT I would support.  Additionally I understand the usefulness of DataSets in a classic (intranet) Client-Server WinForms app with lots of DataBinding. 

Returning DataSets from a publically accessible Web Service is a BAD IDEA©.

Now, why shouldn't we return DataSets from WebServices?  DataSets and their serialized XML format includes a pile of information that has little to do with the Domain Model itself.  DataSets may be DiffGrams, they may or may not include schema, and they represent "Sets of Data."  They are an object of one type, DataSet.  Whether there is a Java version of a DataSet object available doesn't matter.  They are late-bound by nature, as even a Strongly Typed DataSet encapsulates conversion of types back and forth from SqlDataTypes to typical CLR types and calls to Rows["Apple"].  DataSets are the Class equivalent of an Variant - an Object that can be any kind of Object - only serializable as XML.  Returning an object of type DataSet or Typed DataSet via publically accessible Web Service would succeed only in confusing a Java person, stymying any chance of interop, and giving them more ammo to use against .NET.

To be clear, I WOULD architect a system that included DataSets if I felt that they provided an exceptional value.  I'm just promoting that folks BE AWARE of the ramifications of their decisions.


Tagential aside: There's some yummy best practices up on TheServerSide.NET.  Here's my favorites:

  • Using a DataReader vs. a DataSet: The DataReader was of course faster. It was faster by 16% in this particular case.
  • SQLDataReader vs. OleDBDataReader: Going with native drivers is always better. The SQLDataReader was 115% faster than going through OLE.
  • DataReader Column Reference - By Name, Ordinal, or GetString(): The order of speed? dr[0] was the fastest, followed by dr["ProductName"], followed by dr.GetString(0) as it has to do the conversion.
  • Inline (DataReader) vs. Controls (DataGrid): The inline script was 233% faster than a DataGrid component.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

A reminder on "Three/Multi Tier/Layer Architecture/Design" brought to you by my late night frustrations.

June 1, '04 Comments [16] Posted in ASP.NET | Learning .NET | Web Services | DasBlog | XML | HttpHandler
Sponsored By

I was going over some C# code that was written for a non-profit earlier this year.  It was fairly typical code and that's what disturbed me so.  It consisted of a single ASP.NET Application with most of the work happening in the Page_Load.  Although there was only one database, the connection string to that database appeared in at least a dozen places throughout the code.  There were a few places where a SELECT * from WHATEVER occurred and they spun through a DataReader in order to set a boolean called "HasRows" to true. 

So, I had to do something.  I understand that not everyone cares, and that not everyone had (or chose to have) any formal Computer Science or Software Engineering academic life (not that there's anything wrong with that.)

Disclaimer: This post is just general advice.  I know that reams of paper have been written on how to design software, layers, tiers, services, etc.  This post is just to remind a few people that you can't have multiple layers until you start thinking about the responsibility of each layer - the contract and binding.  Life has hierarchies and layers and responsibility - and so should most software.

That being said, here's little reminder about layers of abstraction.  They are typically a good thing.  Remember though, that in "Scott World" (which is hopefully your world also :) ) a "Tier" is a unit of deployment, while a "Layer" is a logical separation of responsibility within code.  You may say you have a "3-tier" system, but be running it on one laptop.  You may say your have a "3-layer" system, but have only ASP.NET pages that talk to a database.  There's power in precision, friends.

  • The Presentation Layer: For the vast majority of programmers who are doing ASP.NET, your pages live for one reason, to present an interface to the user.  That is usually HTML/XML. (ya, ya, I know you might produce WML, CHTML, or GIFs with HttpHandlers but this whole post is a generalization)
    • Things your pages shouldn't know about: Your pages shouldn't know that a physical data store exists.  DasBlog does a lovely job of abstracting away the data store and presenting only an object model to the user.  Your ASP.NET pages shouldn't know about connection strings, Connections, Commands, or anything like that.
    • Things your pages should know about: Your pages should know about your Domain Model.  Your objects, your stuff.  If you're a librarian, I suspect you'll have a Book object.  Perhaps a Librarian Service that knows how to Get and CheckOut books.  He/She (the Service) no doubt knows how to Search for Books also. 
  • The Business Logic 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.)
    • Things your BizLogic Layer should know about: It should know about business rules (This payment will need 5 days of lead-time to be paid, or that book can't be checked out by two people at once) but not the details of storage.  It might present public methods like "GetBookByISBN" or "CheckOutBook(Book b)" or "ReturnBook(Book b)."
  • The Data Access Layer: Depending on how "purist" you want to get, you can merge the DAL with the Business Layer.  At a minimum you should be able to perform CRUD operations.  Create, Read, Update, Delete.  Fundamentally most apps are just GETS of stuff and PUTS of stuff.  Keep it simple.  If your backing data store is XML files, or a Web Service or a Database, it shouldn't matter, and ideally your Business Layer (above) doesn't even know!  It knows there's a DAL to talk to, but it's all about responsibility.  If you are designing a layer, know your in's and out's and for Goodness' Sake know your responsibility.  If you don't, back to the drawing board until you do.
    • Things your Data Access Layer shouldn't know about: It shouldn't know about HTML, and it shouldn't know about ASP.NET.  It should avoid knowing about business rules that don't related directly to how the data is stored (like cardinality.)
    • Things your Data Access Layer should know about: It should know about accessing data in whatever form it's stored.  It might have functions like "InsertBook(string name, string isbn)" or "InsertBook(Book b)" if you don't mind it knowing about your Business Objects (I usually don't).  It should act as an Adapter between your Data Access Layer and your Data Store.
  • The Data Store: This might be a SQL Server; then you'll need CRUD Stored Procedures to ReadBooks, UpdateBooks, etc.
    • Things your Data Store shouldn't know about: Your Data Store shouldn't be generating messages in language.  That means don't have your Stored Procs generate English messages.  Sounds silly, but I've seen it before in the wild, so it bears repeating.  It's your Data Store's job to get data to the DAL, and little else. 
    • Things your Data Store should know about: Storing stuff.  'Nuff said.

This may seem overkill, but believe me, once you've done it a few times, you'll appreciate it when you come back to mess with the code in the future.  Remember also that much of this CRUD stuff can be auto-generated by CodeSmith or by Deklarit.  Please, do resist the urge to open up a SqlConnection from your next ASP.NET page.

Why should you do this?  Because of the 'ilities and because the simplest even-slightly layered design hides complexity. 

  • Flexibility: "The ease with which a system or component can be modified for use in applications or environments other than those for which it was specifically designed."
  • Maintainability: "The ease with which a software system or component can be modified to correct faults, improve performance, or other attributes, or adapt to a changed environment."
  • Reusability: "The degree to which a software module or other work product can be used in more than one computing program or software system."
  • Scalability: "The ease with which a system or component can adjust to changing load."
    [All these definitions are from the IEEE Standard Computer Dictionary.]

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

The Myth of .NET Purity, Reloaded

May 30, '04 Comments [0] Posted in ASP.NET | Learning .NET | TechEd | Web Services
Sponsored By

An updated version of my Myth of .NET Purity rant is up on MSDN.  I recommend you show it to your boss if you're being force to justify why your solution isn't 100% .NET. 

Summary: Does a solution written for Microsoft .NET have to be 100% .NET? Scott Hanselman looks at how hybrid managed-unmanaged solutions are really the norm.

Here's a few choice quotes, IMHO: ;)


I've heard it said by many a CTO in many a technical briefing that, "We're planning to port our whole system to .NET." Why spend 18 months converting your application, so you can arrive at the endpoint you're already at?


The .NET Framework Library itself isn't "pure .NET," as it uses every opportunity to take full advantage of the underlying platform primitives.


If Microsoft were to truly virtualize the machine, they would have marginalized their investment in the Windows platform.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Movies That Are Disasters (or Disaster Movies)

May 30, '04 Comments [5] Posted in Movies
Sponsored By

I shall preface this with the apology you've heard before...I consider this a Technology Blog, and I hope only that the content found within is useful and of high quality.

That said - I give you, after having seen "The Day After Tomorrow," Disaster Movie Mad-Libs:

Open: Obscure nobody scientist who is an expert not in an obscure corner of science (climatology, seismology, genetics) but actually an expert in an obscure corner OF an obscure corner of science (paleoclimatology, seismological tectonophysics, quantitative genetics) is giving a talk at the Met, or the New York Library, or Berkeley (It's usually either a black-tie affair, perhaps the international conference of obscvure scientists, maybe it's in Bern, OR it's an empty auditorium in a random sub-wing of a Community College and there's three people paying attention) with a polished presentation (either using ridiculously advanced full 3D, OR using transparancies and an overhead projector.  Noone ever seems to use PowerPoint in these movies.

Scene: Outside that same talk (usually in the rain, at night, next to a large iron gate) and some one (usually wearing a London Fog standard-spy-issue raincoat, possibly smoking, and if he is smoking, he's smoking like a Nazi with his palm up and the cigarette is in a small plastic holder) comes up and says "I loved your talk, I read your paper on [something hyper-obscure] in the field of (paleoclimatology, seismological tectonophysics, quantitative genetics) and your thoughts [something only someone who cared could know] are revolutionary. 

Scene: We find that the scientist is having trouble at home (surprise) and he doesn't pay enough attention to his (kid, wife, dog, all of the above).  He's obsessed with his field and noone will listen.  He probably flips through a few envelopes that say "Final Notice" on them and opens a fridge that contains only Beer and a bottle of Ketchup.  He kicks a bunch of old (newspapers, scientific papers, hardcover books) aside, and sits down to watch (Knicks, Lakers, Manchester United).  The phone rings...

Scene: "Holy crap, something dramatic has happened in the field of (climatology, seismology, genetics) and birds are (acting weird, killing people, falling out of the sky) and all the animals at the New York Zoo are howling.  Come quick! 

Cut to the President: "Who's the best in this field?" "Uh, (consults a list), Dr. [scientist name], at Berkeley."  "Get him here.  Now."  Cue timpani...

Scene: He steps off of a (plane, government helocopter, limo) into (the white house, a secret (CIA, NASA, NSA, X-Files) base, to give an imprompu talk with even BETTER 3D graphics to a bunch of suits and a bunch of generals.  "Gentlemen, Mr. President.  You should have listened.  I've been presenting at Community Colleges all over the nation on this very subject for 15 years, and noone has listened.  Now, we're screwed if we don't (get some miners to detonate a nuclear bomb underground, run south, get some miners up to that Asteroid, get some miners down to the center of the earth, get Superman to turn back time) immediately.  I give us six to eight (weeks, months, hours)." 

Scene: His (kid, wife, dog, all of the above) are on vacation in an older red pickup in (Yellowstone, Alaska, Mt. St. Helens) and have almost (been swallowed up by the earth, buried by a mudslide, drowned in a flash flood, froze to death). 

Scene: "Dr. [scientist name], your (kid, wife, dog, all of the above) have just almost (been swallowed up by the earth, buried by a mudslide, drowned in a flash flood, froze to death)."

Scene: The (miners, astronauts, army) prepare the (bomb).

Scene: A shot of a helicopter flying over the Statue of Liberty, it's (buried in snow, burning, underwater, covered in lava, a metal carcass) Cue string instruments...

Scene: "I'm going after them."  "You can't! It's suicide!"  Stern serious look..."I made my (kid, wife, dog, all of the above) and I'm not going to let them down."  "I know Jack...that's why I'm (pause for effect) going with you..."

<<Insert Drama Here>>

Scene: The first sunrise since (the earth froze/didn't freeze, the volcano exploded/didn't explode, the lava flowed/stopped, the fires started/stopped, the meteor crashed/didn't crash)  The Scientist turns to his (kid, wife, dog, all of the above) and says "I think we're going to be OK..." 

Did I miss anything?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

The ErgoDex Alternative Keyboard and the Rise of "CodeRush IL"

May 28, '04 Comments [0] Posted in TechEd | CodeRush | Gaming
Sponsored By

When folks say that the differences between C# and VB.NET are "syntactic sugar" I like it.  Sure there are a few funky ; % & ^ things in C#, and every once in a while someone will add a new operator to VB.NET like "AndAlsoIsNotOnTuesday" but generally ifs are ifs and fors are fors, eh?

Since CodeRush lets you map something as innocuous as "c" to a complete class definition and "u" to "using{}" and the whole IDisposable thing in C#, the whole language is just sugar.

If you think "string" that's one syllable.  String.  Boom.  Why type s. t. r. i. n. g.  The whole middle "trin" part of "string" isn't value-added.  So, you type "sg"-space and you get string.

So, I was thinking, since in CodeRush if you select block of code and press "u" it will put "using{ (your code block) } around it, what should it do in VB.NET as they don't have "using."  But, even though VB.NET folks have to put a Try/Finally and explicit call to Dispose(), the semantics are the same.  Why not map "u" in VB.NET in CodeRush to something like:

Dim o As Object
Try
o = New Object()
Finally
If Not o Is Nothing Then
con.Dispose()
End If
End Try

See?  The semantics of "u" are the same regardless of language.  Now, I used to be all about VB.  I spent years getting paid for VB3/4/5.  But it's been a while, and I'm largely a semicolon guy now.  Even in VB.NET I'll sometimes type o = New Object()'; just for comfort (notice the '). 

Often I need to do demos in VB.NET and I don't remember the syntax.  I can, however, remember "c" or "u" or "pdt" (property that returns a DateTime).  I'm calling it "CodeRush IL" and it's making me not only more productive in my own language, but also in VB.NET.

<bigidea>Now, what about a keyboard JUST for the .NET Programmer</bigidea>

If you know me, you know I have a few gadgets.  I'm such an Early Adopter that I preordered Doom III when Castle Wolfenstein came out.

When I saw a new kind of game input device announced at E3, I know I had to have one.  What's the first thing you do when you get a game?  You have to make a conscious effort to marry syntax and semantics.  What's jump? What's crouch? What's "Build Farm"?

The Ergodex is a completely customizable personal input system.  I think of it as a combination tablet and keyboard.  The 25 Keys are LOOSE.  You can arrange them in any order or orientation you want and they will stick to the board.

You can also put templates and information under the plastic cover. 

I'm going to map the keys in whatever what I think is comfortable to various expansions in CodeRush.  I'll have the datatype section of keys, and the flow control section.  I may even hookup a number of keys just to refactor. 

It's going to be hot.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.