Dynamic Return Classes?

Discussion in 'ASP .Net Web Services' started by jasonpaulplank@yahoo.com, Feb 8, 2006.

  1. Guest


    My company is new to Web Services and we are doing some "prototyping"
    with them. My boss wants to be able to have our clients (some .NET,
    some Java, etc) call the web service and get back the appropriate

    If sounds like the perfect application for Web Services; however, what
    "we" need to do is; depending on who is calling and what they are
    calling it with, we want the return values/objects to differ.

    We are a Financial institution so I will give you a "real world"
    example We want to have a GetFundInformation webservice that takes the
    name/acronym of a Fund. Depending on the name of the fund given, we
    need to look up the type of fund (is it a bond fund, municipal fund,
    etc). Different types of funds have different
    attributes/fields/properties (for example, a bond fund might have a
    maturity date but a municipal fund may have a state held) so the return
    types would be different (again, different properties).

    Another consideration is that, depending on who is calling the web
    service, you may or may not be able to see certain attributes of the
    fund. Joe Schmo off the street may not be allowed to see the Unapprove
    Quarterly Interest Rate but someone in our Legal department can.

    Is there a way to dynamically generate a class (and return it) at
    runtime? If so, how well does this work with other environments (ie
    Java)? Someone suggested returning an XMLDocument instead of a class
    but would we lose anything as far as interoperability with other
    environments (wouldn't we have to pass XSD documents to all of our

    Sorry for my ignorance,
    , Feb 8, 2006
    1. Advertisements

  2. Josh Twist

    Josh Twist Guest

    Good post.

    The concept of Dynamic responses can be easily implemented by
    specifying a return type of XmlElement. The you can return any old Xml
    you like.

    BUT... And it's a huge BUT. ...

    Most people, and I totally agree, will tell you this is a terrible
    idea. If someone is going to work with your return type they need to
    know all about it anyway. Web services use WSDL as the means of
    communicating this schema and it's what makes Web Services great.

    I would suggest a strongly-typed return type that caters for the
    different possibilities of object being returned.

    Since XSD is how Web Services are defined you could take advantage of
    the <xsd:choice> idea.

    That is, your return type might be called Fund and and has an enum
    FundType (which can be one of BondFund or MunicipalFund). There are
    then two additional properties of type BondFund and MunicipalFund which
    contain the relevant shape and data for each (only one would be

    Read the first Q&A on this post for more help:

    This is just one possible solution to your problems. Whatever you do,
    be sure to share your schema (or contract) through web services and
    don't just return a string that then has to be parsed by your client,
    through some external understanding. For me, that just isn't what web
    services are about.

    If you have any questions, just shout :)

    Josh Twist, Feb 9, 2006
    1. Advertisements

  3. Guest


    Thanks for the help. I'll take a look at the link you mentioned above
    and post back if I have any problems/questions.

    Thanks again,
    , Feb 9, 2006
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Jul 22, 2005
  2. lonelyplanet999
    Nov 13, 2003
  3. Carfield Yim
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Andrew Thompson
    Jul 17, 2004
  5. Greenhorn
    Keith Thompson
    Mar 6, 2005

Share This Page