Design Guidelines (2)
Many service requests over the Internet may affect the performance of your applications. When designing your XML Web Service, you can effectively use service requests by creating a method that aggregates relevant information. For example, assume that you have an XML Web Service used to retrieve information from a book. We can create a method to return all the information in a service request, instead of a separate method to retrieve the title, author, and publisher. One-time transmission of large pieces of information is more efficient than multiple transmission of small pieces of information.
The following code example explains how to organize the information to a single XML Web Service method.
[C #] <% @ WebService Language = "C #" Class = "DataService" %> Using System; Using System. Data; Using System. Data. SqlClient; Using System. Web. Services; Public class DataService { [WebMethod] Public DataSet GetTitleAuthors (){ SqlConnection myConnection = new SqlConnection ("Persist Security Info = False; Integrated Security = SSPI; server = localhost; database = pubs "); SqlDataAdapter myCommand1 = new SqlDataAdapter ("select * from Authors", myConnection ); SqlDataAdapter myCommand2 = new SqlDataAdapter ("select * from Titles", myConnection ); DataSet ds = new DataSet (); MyCommand1.Fill (ds, "Authors "); MyCommand2.Fill (ds, "Titles "); Return ds; } } [Visual Basic] <% @ WebService Language = "VB" Class = "DataService" %> Imports System Imports System. Data Imports System. Data. SqlClient Imports System. Web. Services Public Class DataService <WebMethod> _ Public Function GetTitleAuthors () As DataSet Dim myConnection As New SqlConnection ("Persist Security Info = False; Integrated Security = SSPI; server = localhost; database = pubs ") Dim myCommand1 As New SqlDataAdapter ("select * from Authors", myConnection) Dim myCommand2 As New SqlDataAdapter ("select * from Titles", myConnection) Dim ds As New DataSet () MyCommand1.Fill (ds, "Authors ") MyCommand2.Fill (ds, "Titles ") Return ds End Function End Class |
When designing your XML Web Service, make sure to use standard object-oriented programming operations. Use encapsulation to hide implementation details. For more complex XML Web Services, you can use inheritance and Polymorphism to re-use code and simplify your design.
The following code example shows how to use inheritance to create an XML Web Service that executes mathematical computing.
[C #] <% @ WebService Language = "C #" Class = "Add" %> Using System; Using System. Web. Services; Abstract public class MathService: WebService { [WebMethod] Abstract public float CalculateTotal (float a, float B ); } Public class Add: MathService { [WebMethod] Override public float CalculateTotal (float a, float B) { Return a + B; } } Public class Subtract: MathService { [WebMethod] Override public float CalculateTotal (float a, float B) { Return a-B; } } Public class Multiply: MathService { [WebMethod] Override public float CalculateTotal (float a, float B) { Return a * B; } } Public class Divide: MathService { [WebMethod] Override public float CalculateTotal (float a, float B) { If (B = 0) Return-1; Else Return a/B; } } [Visual Basic] <% @ WebService Language = "VB" Class = "Add" %> Imports System Imports System. Web. Services MustInherit Public Class MathService: Inherits WebService <WebMethod> _ Public MustOverride Function CalculateTotal (a As Single ,_ B As Single) As Single End Class Public Class Add: Inherits MathService <WebMethod> Public Overrides Function CalculateTotal (a As Single, B As Single) As Single Return a + B End Function End Class Public Class Subtract: Inherits MathService <WebMethod> Public Overrides Function CalculateTotal (a As Single, B As Single) As Single Return a-B End Function End Class Public Class Multiply: Inherits MathService <WebMethod> Public Overrides Function CalculateTotal (a As Single, B As Single) As Single Return a * B End Function End Class Public Class Divide: Inherits MathService <WebMethod> Public Overrides Function CalculateTotal (a As Single, B As Single) As Single If B = 0 Then Return-1 Else Return a/B End If End Function End Class |