Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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


advertisement
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 = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class CategoryService : System.Web.Services.WebService { [WebMethod] public List<Category> GetCategories() { CategoryDB category = new CategoryDB(); return (List<Category>) category.GetCategories(); } }

 
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.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap