Middle Tier Code


Making Web Services More Efficient

This section looks at two ways of making Web services more efficient. The first involves creating another class within the service to act as a container for values to be returned. The second looks at consolidating several method calls into one Web method to reduce network traffic.

Creating Another Class

A class who has no methods and all public variables provides for a handy container for passing back values to the consumer. This guarantees that you use a data structure that can be represented in SOAP without error. Consider the following example.

    public class ReturnValues      {       public string id;       public string password;       public string email;       public string title;       public string FirstName;       public string LastName;       public string moneyName;       public double returnAmountUs;       public double returnAmountForeign;      } 

Now there is a container for storing the data that can be easily accessed and passed back. Because all the variables are public there is no need for having accessor methods.

By putting this class in a Web Service, it creates the following construct in the WSDL. Note there is no means to serialize methods, only attribute variables of a class. This needs to be in the WSDL so the proxy, when generated, knows how to handle this user-defined type.

<s:complexType name="ReturnValues"> <s:sequence>  <s:element minOccurs="0" maxOccurs="1" name="id" type="s:string" />  <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string" />  <s:element minOccurs="0" maxOccurs="1" name="email" type="s:string" />  <s:element minOccurs="0" maxOccurs="1" name="title" type="s:string" />  <s:element minOccurs="0" maxOccurs="1" name="FirstName" type="s:string" />  <s:element minOccurs="0" maxOccurs="1" name="LastName" type="s:string" />  <s:element minOccurs="0" maxOccurs="1" name="moneyName" type="s:string" />  <s:element minOccurs="1" maxOccurs="1" name="returnAmountUs" type="s:float" />  <s:element minOccurs="1" maxOccurs="1" name="returnAmountForeign" type="s:float" />  </s:sequence>       </s:complexType>

This WSDL code then generates the following code in the proxy.

    [System.Xml.Serialization.XmlTypeAttribute      (Namespace="http://www.advocatemedia.com/")]      public class ReturnValues {         /// <remarks/>         public string id;         /// <remarks/>         public string password;         /// <remarks/>         public string email;         /// <remarks/>         public string title;         /// <remarks/>         public string FirstName;         /// <remarks/>         public string LastName;         /// <remarks/>         public string moneyName;         /// <remarks/>         public System.Single returnAmountUs;        /// <remarks/>       public System.Single returnAmountForeign;    }

Then, when you call a Web Service that contains this class, you must create it before you pass any values to it. In the following code snippet, myReturnValues is an object of the ReturnValues type. Notice that it is very easy to display data from this object in the Console.WriteLine statement.

    ReturnValues myReturnValues = new ReturnValues();     Service1 myService = new Service1();     myReturnValues = myService.testReturn();     Console.WriteLine("This is the email " +     myReturnValues.email);

This creates an handy container for passing values back and forth to the Web Service.

Making Methods Efficient

The point of making methods more efficient is to reduce the amount of traffic occurring on the network. The best way to do that is to encapsulate a unit of work within the method. This means that you put everything you need to do with one Web Service inside one method. Consider the doAll method in the following code snippet.

    [WebMethod     (Description="Get information from ID and return money      info")]      public ReturnValues doAll(string id, string country,      float usDollars, float foreignDollars)      {        ReturnValues myReturnValues = new ReturnValues();        myReturnValues.moneyName =        myExchange.returnCurrencyName(country);        myReturnValues.returnAmountUs =        myExchange.returnUSDollarEquiv        (country, foreignDollars);        myReturnValues.returnAmountForeign =        myExchange.returnForeignEquiv(country, usDollars);        Hashtable myHashTable = new Hashtable();        GetXmlLibrary myGetXml = new GetXmlLibrary();        myHashTable = myGetXml.getData(id);        myReturnValues.id = (string)myHashTable["id"];         myReturnValues.password =        (string)myHashTable["password"];        myReturnValues.email    = (        (string)myHashTable["email"];        myReturnValues.title    =        (string)myHashTable["title"];        myReturnValues.FirstName =       (string)myHashTable["FirstName"];        myReturnValues.LastName =       (string)myHashTable["LastName"];        return(myReturnValues);      } 

The previous method takes all possible methods from the Web Service and puts them in the same method and passes all the information back to the consumer. This reduces the number of Web requests and response by two, which Figure 12.5 demonstrates.

click to expand
Figure 12.5: Putting the unit of work within a single method reduces the amount of traffic.

Or if that’s too much, there’s the doAllMoney method in the following code snippet that only works on exchanging money.

    [WebMethod(Description="Do all money related calls")]     public ReturnValues doAllMoney     (string country, float usDollars, float foreignDollars)     {      ReturnValues myReturnValues = new ReturnValues();      myReturnValues.moneyName =       myExchange.returnCurrencyName(country);       myReturnValues.returnAmountUs =       myExchange.returnUSDollarEquiv(country, foreignDollars);       myReturnValues.returnAmountForeign =       myExchange.returnForeignEquiv(country, usDollars);       return(myReturnValues);     }

The following code example is the complete modified Web Service so you can see the placement of the ReturnValues class.

Note

The hash used in the XML methods could have been an object too, but it is used here to show that you need to test the types you return from objects in libraries to ensure they can be passed back by Web Services.

using System;     using System.Collections;     using System.ComponentModel;     using System.Data;     using System.Diagnostics;     using System.Web;     using System.Web.Services;     using XmlSearchString;    namespace MiddleTier    {     public class ReturnValues     {      public string id;      public string password;      public string email;      public string title;      public string FirstName;      public string LastName;      public string moneyName;      public double returnAmountUs;      public double returnAmountForeign;      }     [WebService(Description="XPlatform Middle Tier Example",      Namespace="http://www.advocatemedia.com/")]      public class Service1 : System.Web.Services.WebService      {         public Service1()         {          InitializeComponent();         }           #region Component Designer generated code           //Required by the Web Services Designer           private IContainer components = null;           private void InitializeComponent()           {           }           protected override void Dispose( bool disposing )           {              if(disposing && components != null)              {               components.Dispose();              }             base.Dispose(disposing);           }           #endregion           private advocatemedia.MoneyExchangeService myExchange =             new advocatemedia.MoneyExchangeService();           [WebMethod(Description="check if id exists")]           public int ValidateId(string id)           {             GetXmlLibrary myGetXml =                  new GetXmlLibrary();             Hashtable myHashTable = new Hashtable();             myHashTable = myGetXml.getData(id);             string returnValue =                  (string)myHashTable["id"];             int result =                  string.Compare(id,returnValue);             //weird -- 0 if true             return(result);           }      <DIS[WebMethod              (Description="Return Email for Give Address")]           public string ReturnEmail(string id)           {             Hashtable myHashTable = new Hashtable();             GetXmlLibrary myGetXml =                  new GetXmlLibrary();             myHashTable = myGetXml.getData(id);             return((string)myHashTable["email"]);           }           [WebMethod(Description="Return Password for given id")]           public string ReturnPassword(string id)           {             Hashtable myHashTable = new Hashtable();             GetXmlLibrary myGetXml =                  new GetXmlLibrary();             myHashTable = myGetXml.getData(id);             return((string)myHashTable["password"]);           }           [WebMethod                (Description="Return all data as an array")]           public ReturnValues ReturnAll(string id)           {             Hashtable myHashTable = new Hashtable();             GetXmlLibrary myGetXml =                  new GetXmlLibrary();             myHashTable = myGetXml.getData(id);               ReturnValues myReturnValues =                    new ReturnValues();               myReturnValues.id       =                    (string)myHashTable["id"];               myReturnValues.password =               (string)myHashTable["password"];               myReturnValues.email    =                    (string)myHashTable["email"];               myReturnValues.title    =                   (string)myHashTable["title"];             myReturnValues.FirstName =               (string)myHashTable["FirstName"];               myReturnValues.LastName =               (string)myHashTable["LastName"];             return(myReturnValues);           }      [WebMethod               (Description="Get the name of the currency")]           public string ReturnCurrencyName                (string countryName)           {               string moneyName =               myExchange.returnCurrencyName(countryName);             return(moneyName);           }      [WebMethod              (Description="Return the Foreign Equivalent")]           public double ReturnForeignEquiv               (string country, float amount)           {            double returnedAmount =              myExchange.returnForeignEquiv                  (country, amount);              return(returnedAmount);           }           [WebMethod                (Description="Return the US Dollar Equiv ")]           public double ReturnUSDollarEquiv             (string country, float amount)           {            double returnedAmount =              myExchange.returnUSDollarEquiv                   (country, amount);            return(returnedAmount);           }           [WebMethod                (Description="Get information and return money")]           public ReturnValues doAll          (string id, string country, float usDollars,              float foreignDollars)           {             ReturnValues myReturnValues =                  new ReturnValues();               myReturnValues.moneyName =               myExchange.returnCurrencyName(country);             myReturnValues.returnAmountUs =               myExchange.returnUSDollarEquiv                    (country, foreignDollars);               myReturnValues.returnAmountForeign =               myExchange.returnForeignEquiv                   (country, usDollars);             Hashtable myHashTable = new Hashtable();             GetXmlLibrary myGetXml =                  new GetXmlLibrary();             myHashTable = myGetXml.getData(id);             myReturnValues.id       =                 (string)myHashTable["id"];             myReturnValues.password =               (string)myHashTable["password"];             myReturnValues.email    =                 (string)myHashTable["email"];             myReturnValues.title    =                 (string)myHashTable["title"];               myReturnValues.FirstName =               string)myHashTable["FirstName"];               myReturnValues.LastName =               (string)myHashTable["LastName"];             return(myReturnValues);           }           [WebMethod                (Description="Do all money related calls")]           public ReturnValues doAllMoney            (string country, float usDollars, float foreignDollars)           {            ReturnValues myReturnValues = new ReturnValues();            myReturnValues.moneyName =              myExchange.returnCurrencyName(country);            myReturnValues.returnAmountUs =              myExchange.returnUSDollarEquiv(country, foreignDollars);            myReturnValues.returnAmountForeign =              myExchange.returnForeignEquiv(country, usDollars);            return(myReturnValues);           }         }       }

Methods that do many things are excellent for encapsulating a unit of work with a Web Service.

Tip

Another thing to consider with a Web Service is what you are using it for. For example, you would not want to use a Web service for session management or to keep track of state (logged in/logged out) on a Web site. That requires too much traffic, especially when you constantly need to communicate with the database. Web Services can be used for setting states that are cookie based, this is what the following set of examples shows.




Cross-Platform Web Services Using C# and Java
Cross-Platform Web Services Using C# & JAVA (Charles River Media Internet & Web Design)
ISBN: 1584502622
EAN: 2147483647
Year: 2005
Pages: 128

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net