Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Lightweight Collections from Web Service Methods


Lightweight Collections from Web Service Methods

Like the Recordset in ADO, the ADO.NET DataSet object is one of the most loved and used managed data types. Not only is the DataSet a rich and powerful programming interface, it also enjoys very special treatment from data-bound controls. The Windows Forms DataGrid control, for example, can automatically navigate through the various tables of a DataSet. With a little code, you can obtain the same from a web DataGrid, too.

It is common practice to create .NET XML web services with methods that accept and return DataSet objects. As a developer, you reason in terms of a powerful and feature-rich object; the surrounding framework completes the scenario by letting you connect it to a variety of controls in a pretty seamless way. As a matter of fact, the DataSet shines. But is all that shines necessarily gold?

The key thing going on here is that the DataSet is a platform-specific object. On the other hand, the web service is a cross-platform tool. This means that a web service created using the .NET Framework can easily be consumed by clients living on non-.NET platforms, including a Java platform.

Web services are known to be interoperable components, but not at all levels. Interoperability is guaranteed at the XML/SOAP level, but this is not necessarily good news for all developers. A much better form of interoperability is the toolkit interoperability, in which the schema of involved objects can easily be adapted to the caller platform. The DataSet is a weakly typed, polymorphic type whose actual layout can be fully determined only when the object is filled with data. This makes it difficult for a type converter component to analyze its general-purpose schema and come up with a class. Note that this is a general problem that also exists in the .NET platform but that finds a slick workaround in the .NET Framework.

To make your web service really interoperable at a level higher than the raw XML API, you should avoid using DataSets. If you find it a really comfortable type, opt for a typed DataSet instead. A typed DataSet provides a strongly typed interface, which simplifies the job to other platforms' class builders. A class builder is a tool that reads a WSDL file and creates a corresponding class. A class is normally an offline tool; in .NET, it is the wsdl.exe tool.

As an alternative to using typed DataSets, you can opt for a collection or an array of custom types. For example, if the method is expected to return all orders for a given customer, you can pack all the orders in an array. You represent each order with a made-to-measure structure. The .NET web service infrastructure-specifically the code behind the WebMethod attribute-takes care of serializing the array to XML.

The XML representation of an array is much simpler to parse for a class builder and it also significantly reduces the number of bytes transferred across the wire. The only problem left is with data binding. An array or a collection is not immediately bindable to all .NET controls. The .NET class builder builds a client-side representation of the custom class but, for simplicity, it renders all data members with fields instead of properties.

Public Class Person
   Public Name As String
End Class

A field is simply a public property; a property has get/set accessors. You must replace the code above with an equivalent code that uses properties with get/set accessors.

Public Class Person
   Private _name As String
   Public Property Name As String
      Get
          Return _name
      End Get
      Set (Value As String)
          _name = Value
      End Set
   End Property
End Class

The class declaration is normally found in the reference.vb file generated by the Web Reference wizard in Visual Studio .NET. Once you have modified the reference.vb file, recompile the project. The data binding will now work just fine.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.