Best Practice, How should I return Web Service data in this case?

Best Practice, How should I return Web Service data in this case?

Old forum URL: forums.lhotka.net/forums/t/3891.aspx


ltgrady posted on Tuesday, November 13, 2007

I tried this question over at asp.net forums and got no response.  It's a little off topic but everything we have is developed on CSLA (1.5 still until we bit teh upgrade bullet) so not terribly so.

Our main app uses asp.net/c#/CSLA 1.5/SQL Srvr 2k5, but now our users would like to start integrating some of our search functionality and use our product database in their web pages.  This seemed like an obvious place to start using web services.  We have been working through a couple iterations of our web service. We started out returning strings and other simple data types.  Then we moved to returning data transfer objects that we typed out.

However, we had a method that required the fields returned to be dynamic.  The user passes in a FieldGroupID and based on that field group we would return back a meta-data driven set of fields.  There are 400 possible fields, so a fieldgroup may return back a logical collection of those fields, maybe a dozen or so.  We found the way that returned the fastest results was to use use .ExecuteXmlReader which called a SQL stored procedure containing FOR XML transact SQL.  It uses the meta data tables, with the fieldgroup we pass in, to create an xml result with JUST the fields we require instead of loading tons of fields we don't need.  We loaded that right into an xmlReader to an xmlDocument and returned that in our web service.  We were somewhat forced to do it that way because of the nature of the dynamic fields but it turns out that it's bringing back a really quick result and seems very optimized.

So now we're looking at our other methods which return back strongly typed DTO classes. The fields are not dynamic but fixed. I'd like to speed them up using the above method, but then my wsdl file is going to say sequence: "any" and we're not going to be providing and real information for our consumers.

Is there a standard accepted practice we should or could be using to do this properly that gives our users the most information and returns back the quickest result?  We're pretty new to web services and learning as we're going.  I would appreciate any advice, thanks.  Larry

richardb replied on Wednesday, November 21, 2007

In the past I created multiple versions of the web services, one returning XML the other returning a Strongly typed DTO.  That way the user of your service can decide which ones best for them.

As for the XML I didn't get it straight from the database as I had the data in my business objects so just manauly created the XML to my preferred schema - I had more control over it at this point and found it easier than mucking about with the FOR XML statement in SQL.  But I went for maintainability rather than performance here, although the performance was fine any how.

Could you could transform the data with a stylesheet perhaps if you want to enforce some more rules into the XML like sequence - XSLT stylesheets (powerful but also tricky to write sometimes)?

Richard.

Copyright (c) Marimer LLC