August 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Combine Polymorphism and Web Services

  • January 14, 2005
  • By Paul Kimmel
  • Send Email »
  • More Articles »

As you can see, neither version contains any proprietary information. Web services don't judge or even require that you have proprietary technology to preserve; preserving proprietary business rules is simply a byproduct, because consumers get a proxy class that for all intents and purposes is a structure with no methods.

What SPROXY does roughly is reflect the Web method's types and convert public properties to public fields. This is sufficient for sending and receiving data between client and server via the Web service.

In addition, things such as typed collections are converted to typed arrays. For instance, a PersonCollection that inherits from System.Collections.CollectionBase (see my book Visual Basic .NET Power Coding from Addison Wesley for more on typed collections) would be proxy-generated as an array of Person, or Person(). Here is the rub: What if the Person class were an abstract class and the PersonCollection was intended to be used as a collection of anything derived from Person, such as Employee or Customer? Without some special help, XML Web services would generate a Person proxy class but would not know anything about Employee or Customer (see Listing 3). This technically means that if you returned an array of Employees to satisfy the Person collection, the consumer application would compile but blow up at runtime.

Listing 3. Contains a Definition for a PersonCollection and Person, Employee, and Customer Classes

using System;
using System.Reflection;
using System.Diagnostics;
using System.Xml.Serialization;

namespace BusinessCollections
{
  [Serializable()]
  public class PersonCollection : System.Collections.CollectionBase
  {
    public static PersonCollection CreateNew()
    {
      PersonCollection persons = new PersonCollection();
      persons.Add(new Person("Paul"));
      persons.Add(new Customer("David", "Cadyville"));
      persons.Add(new Employee("Kathy", 50000M));
      
      return persons;
    }

    public Person this[int index]
    {
      get{ return (Person)List[index]; }
      set{ List[index] = value; }
    }

    public int Add(Person value)
    {
      return List.Add(value);
    }

    public PersonCollection Select(Type type)
    {
      PersonCollection persons = new PersonCollection();
      foreach(Person p in List)
      {
        if( p.GetType().Equals(type))
          persons.Add(p);
      }

      return persons;
    }

    public void Dump()
    {
      
      foreach(Person person in this)
      {
        Debug.WriteLine(string.Format("Type: {0}",
                                      person.GetType().FullName));
        PropertyInfo[] properties = person.GetType().GetProperties();
        foreach(PropertyInfo p in properties)
        {
          try
          {
            Debug.WriteLine(string.Format("Name: {0}, Value: {1}",
              p.Name, p.GetValue(person, null)));
          }
          catch
          {
            Debug.WriteLine(string.Format("Name: {0}, Value: {1}",
                                          p.Name, "unknown"));
          }
        }
      }
    }
  }

  
  public class Person
  {
    private string name;

    public Person(){}

    public Person(string name)
    {
      this.name = name;
    }

    public string Name
    {
      get{ return name; }
      set{ name = value; }
    }

    public string GetUpperName()
    {
      return name.ToUpper();
    }

    public string UpperName
    {
      get{ return GetUpperName(); }
      set{}
    }

  }

  public class Employee : Person
  {
    private decimal salary;

    public Employee() : base(){}
    public Employee(string name) : base(name){}
    public Employee(string name, decimal salary) : base(name)
    {
      this.salary = salary;
    }

    public decimal Salary
    {
      get{ return salary; }
      set{ salary = value; }
    }
  }

  public class Customer : Person
  {
    private string city;

    public Customer() : base(){}
    public Customer(string name) : base(name){}
    public Customer(string name, string city) : base(name)
    {
      this.city = city;
    }

    public string City
    {
      get{ return city; }
      set{ city = value; }
    }
  }
}

The important part of Listing 3 is that Employee and Customer are derived from Person, but the Web service knows only about person collections. Also, note that PersonCollection creates an instance of Person, Employee, and Customer in the static method PersonCollection.CreateNew for demonstration purposes.





Page 2 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel