Dynamic Return Classes?

J

jasonpaulplank

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
 
J

Josh Twist

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/
 
J

jasonpaulplank

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top