KnowDotNet

Minimize Code Changes When Changing URL of WebService

Use Function Call as Method Qualifier When Object Type Changes

by Les Smith

How can I minimize the lines of code that must change when I need to change the URL for a Web Service object?  Use a Function call as the object to qualify the Web Service Method call rather than creating an object representing the Web Service.

I recently put a relatively complex application into production.  The client application uses a Web Service to call Remote Objects to get data from the database.  This application is a DashBoard Monitoring application that management uses to monitor operations in the production facility.  Since there are multiple users of this client application, I developed a Cache Object that periodically updates itself so that multiple users don't cause extra database hits.  

The client application is using threading to keep the UI active while retrieving data from the remote object, which can take several seconds to retrieve all of the data for the various grids, graphs, and other stats shown on the main form.  There are dozens of places throughout the application where the Web Service is called.  I was creating an object of the Web Service in numerous places, in the form of the code shown below:

   Private Function GetData() As DataTable
      
Dim ws As New remotepAppServ.MyWebService
      
Dim ds As DataSet =
          ws.GetJobsByHour( _
          
Me.dtpStartDate.Value, DateAdd(DateInterval.Day, 1, _
          
Me.dtpEndDate.Value))
      
Dim dt As DataTable = ds.Tables(0)
      
Return dt
  
End Function

The problem came when I needed to switch back to my local Web Server to do some debugging.  That would have required that I change every line where the 'ws' object was declared, to point to the different server, as shown in the code below.  You can see that I changed the Dim of 'ws' to 'MyAppServ' from 'remoteAppServ'.  I knew I had to change all the references once, but I did not want to make a habit of it.


   Private Function GetData() As DataTable
      
Dim ws As New MyAppServ.MyWebService
      
Dim ds As DataSet =
          ws.GetJobsByHour( _
          
Me.dtpStartDate.Value, DateAdd(DateInterval.Day, 1, _
          
Me.dtpEndDate.Value))
      
Dim dt As DataTable = ds.Tables(0)
      
Return dt
  
End Function

To get around the problem of multiple changes, I wrote a Function, called GetWebServiceObject, to return the WebService object.  The code for this method is shown next.

   Public Function GetWebServiceObject() As infoproappserv.InfoProWebService
      
' Return New localhost2.MyWebService
      Return New RemoteAppServ.MyWebService
  
End Function

I cannot get completely away from changing the server name, but I have now confined it to one line of code instead of many.  The next problem is to eliminate all of the declarations of the object except in the GetWebServiceObject.  I had to do that because the declaration would have to be of a different type when I changed Web Servers.  I accomplished this by removing all declarations of the object and replace the object reference, which qualifies the WebMethod call.  The secret to minimizing code changes is to keep the declaration of the object to a minimum and in this case only once.  As you can see, in the code below, I do not instantiate an object of the WebService locally.  I use the object returned by GetWebServiceObject(), which is a shortcut that creates an object without having to Type it locally.

   Private Function GetTranscribedData() As DataTable
      
Dim ds As DataSet = _GetWebServiceObject(). _
         GetJobsByHour(M
e.dtpStartDate.Value, _
         DateAdd(DateInterval.Day, 1,
Me.dtpEndDate.Value))
      
Dim dt As DataTable = ds.Tables(0)
      
Return dt
  
End Function

Obviously, this is not rocket science, but it is a little tip that can save you code changes in the future