RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Serializing, Consuming, and Binding Generics in .NET 2.0 : Page 4

Generics are a powerful new feature in .NET 2.0, but it's not so obvious how to use them as return values from Web services, or how to bind them to controls. Find out how to serialize, data bind, and consume generic collections in ASP.NET 2.0

Returning a Generic Collection from a Web Service
So far, you have seen how to perform data binding with a generic collection returned by the data access layer object. This section discusses how to return generic collections from a Web service, which has the advantage of letting client applications bind directly to the output returned by the Web service method.

The following code shows how to return a generic collection from a Web service. It leverages the CategoryDB class you've already seen to retrieve data from the AdventureWorks database.

   <%@ WebService Language="C#" 
     Class="CategoryService" %>
   using System;
   using System.Web.Services;
   using System.Collections.Generic;
   [WebService(Namespace =  
   [WebServiceBinding(ConformsTo =  
   public class CategoryService  : 
     System.Web.Services.WebService {
     public List<Category> GetCategories() 
       CategoryDB category = new 
       return  (List<Category>)
Figure 2. CategoryService Output: The figure shows the XML for the generic collection object returned by the CategoryService Web service. Note that the Web service returns the collection as an array of Category objects.
In the preceding code, the GetCategories() method simply invokes the GetCategories() method of the CategoryDB class and returns the result to the Web service consumer.

Now, if you browse to the Web service using the browser and follow the on-screen directions to invoke the GetCategories() method, you'll see the output shown in Figure 2.

As you can see from the above figure, the .NET framework serializes the generic collection as an array of Category objects when invoked from the browser. You can then easily bind this array of Category objects to data bound controls in the client application.

Keep It Short
As you can see, the ability to leverage generics as the middle tier return type opens up a whole avenue of new and interesting opportunities to work with data in the user interface layer. Using the techniques shown here, you can now easily serialize, deserialize, and databind generic types and collections. In addition, you can also return generic types from your Web service that can then be seamlessly consumed by client applications.

Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date