Dynamic Return Classes?

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

  1. Guest

    All,

    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
    information.

    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
    consumers)?

    Sorry for my ignorance,
    Jason
    , Feb 8, 2006
    #1
    1. Advertising

  2. 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
    populated).

    Read the first Q&A on this post for more help:
    http://msdn.microsoft.com/msdnmag/issues/03/06/XMLFiles/

    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
    http://www.thejoyofcode.com/
    Josh Twist, Feb 9, 2006
    #2
    1. Advertising

  3. Guest

    Josh,

    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,
    Jason
    , Feb 9, 2006
    #3
    1. Advertising

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
    Replies:
    2
    Views:
    4,927
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,201
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,400
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    899
    Andrew Thompson
    Jul 17, 2004
  5. Greenhorn
    Replies:
    15
    Views:
    801
    Keith Thompson
    Mar 6, 2005
Loading...

Share This Page