Problem Statement


With the programming model that was just presented, the client ends up programming directly against the service provider (SimpleCalculator, in this case), instead of against a generic abstraction of the service. A better approach is for the SimpleCalculator web service to be polymorphic with a service abstractionthat is, an interface. Programming against an interface rather than a particular service implementation enables the client to switch between different providers, with minimal or no changes. This way, the client becomes indifferent to changes in the service provider. For example, imagine the client wants to switch from SimpleCalculator to a different calculator web service, called ScientificCalculator, that supports the same interface as SimpleCalculator but is perhaps more accurate, faster, or cheaper. Ideally, either the client or the service providers would agree to define an abstract calculator interface, the ICalculator interface:

     [WebInterface]//Imaginary attribute. Does not exist in .NET.     public interface ICalculator     {        int Add(int argument1,int argument2);        int Subtract(int argument1,int argument2);        int Divide(int argument1,int argument2);        int Multiply(int argument1,int argument2);     }

If such a web interface were available, the client could code against the interface definition, not a particular implementation, as shown in Example A-3.

Example A-3. Web services client-side interface-based programming model
     ICalculator calculator = new ScientificCalculator(  );     //or     ICalculator calculator = new SimpleCalculator(  );     //This part of the client code is polymorphic with any provider of the service:     int result = calculator.Add(2,3);     Debug.Assert(result == 5);

The next generation of Microsoft distributed application technology, code named Indigo, connects logical services using multiple transports, including web services. Indigo enforces the separation of interface from implementation by using the notion of an abstract contract definition, manifested by .NET interfaces.


The only thing that changes in the client's code when it switches between service providers is the line that decides on the exact interface implementation to use. You can even put that decision in a different assembly from that of the main client's logic and pass only interfaces between the two assemblies. The client can also use a class factory to create the object and get back an interface. There are other benefits to interface-based web services, too; for example, the client can publish the interface definition, making it easier for different service vendors to implement the client's requirements.



Programming. NET Components
Programming .NET Components, 2nd Edition
ISBN: 0596102070
EAN: 2147483647
Year: 2003
Pages: 145
Authors: Juval Lowy

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