Inheritance in types returned by a WebService - good or bad idea?

Simon Guest posted a list of his Top Ten Tips for WebService Interoperability which is a wonderful resource if you are building a webservice that will be widely used. 

Unfortunately the list doesn't advise about inheritance in the types that are returned from a webservice.  This sounds like a no-no ... why would you make something any more complicated than it needs to be, right?  However, it can often make a lot of sense in your business data.  For example, say you have a large object graph for input to one of WebMethods - another WebMethod returns the same data plus more output information.  Wouldn't it make sense to have a specialized subclass that inherited from the base object and provided the extra output information?  We tried this but got burned alive trying to define the schema validation for the output data.

Contrived Example:

[WebMethod]
public int PlaceOrder(OrderRequest order)
{   // return order number
}

[WebMethod]
public Order RetrieveOrder(int orderNumber)
{   // return order with additional info
}

public class OrderRequest
{
   public int Quantity;
}

public class Order : OrderRequest
{
   public string CurrentStatus;
}

<xs:complexType name="OrderRequest">
  <xs:sequence>
    <xs:element name="Quantity" type="xs:int" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="Order">
  <xs:complexContent>
    <xs:extension based="OrderRequest">
      <xs:sequence>
         <xs:element name="CurrentStatus" type="xs:string" />
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

I am having a difficult time getting it to work correctly within a .NET environment.  Are other people doing things like this?  Does it work on other platforms?

1 Comment

  • I should point out that the web method was passing the abstract base as in:



    [WebMethod]

    public void CreateProduct(HostingProduct Product)

    {

    }



    In this example HostingProduct was the abstract base and we were actually using 5-6 products that inherited from that base. This worked very well between .NET and J2EE.



    We also always used SOAPExceptions whenever things went wrong rather than returning, say an int response code...

Comments have been disabled for this content.