Browse DevX
Sign up for e-mail newsletters from DevX


XML Web Services: Just Another Data Tier? : Page 4

Many developers build a Web site and later, add XML Web services as separate projects, resulting in two code bases. This article shows you how to reuse XML Web service to provide data to your own applications.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Reusing Web Service Code
Here's another example of using Web services in both local and remote mode.

In a hypothetical online book store, it's logical to provide "search by author" (see Figure 4) functionality both within the site and externally—and it's not logical to write the search functionality twice.

Figure 4: The sample application exposes a Web service that lists books by author.
The page in Figure 4 uses the api class' ListBooksByAuthor method, which accepts an author's name as input and returns a DataSet. The DataSet format matches Amazon's Web service description (WSDL) because I used their Web service to get some real world data. You can find the XML document containing the data for that DataSet in the sample project as the file petzold.xml. The Web Form Authors.aspx contains a populateGrid() function which creates an instance of the api class, and then sets the DataSource property of a grid control to the result of the ListBooksByAuthor() method, thus binding the grid to the DataSet.

Private Sub PopulateGrid() Dim oSearch As New api() With DataGrid1 .DataSource = oSearch.listBooksByAuthor _ ("Charles Petzold").Tables("Details") .DataBind() End With End Sub

When you click the button labeled "Books by Charles Petzold," the page sets the grid's DataSource property to the PopulateGrid method, which populates the grid with exactly the same data as external partners see by using the Web service (see Figure 5). Of course, making both internal and external (Web service) method results identical may not always be what you want. You can easily tailor the result by including a required partner ID parameter in the Web service method call, letting you differentiate between internal and external callers.

Figure 5: The Web service listBooksByAuthor returns the same dataset whether you run it as a local method or as a Web service.
By presenting and consuming your own search, list, and other methods as both local methods that your application uses, and as XML Web services, other sites can use those services to present data on your behalf—and you don't have to do any extra work to make that happen. Better yet, you can add new functionality without having to maintain code in two places.

Brian J Thomson has been in the computer and communications business since 1978 and has worked for many large corporations as well as small start-ups. He has been using .NET since the beta releases. As a database developer at Electronic Arts he built various product Web sites in .NET linked by XML Web services. He is a big fan of Medal Of Honour: Allied Assault. To reach him by e-mail, .
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date