Web Services should only have a single "parameter"

Today while I was coding away on this Java project I'm working on I realized I needed a little .NET love so I decided to bring up .NET Rocks and listen to the Clemens Vasters interview.  As you'd expect the focus of the interview was on services.  One of the things that was just covered was that Web Services should only accept a single parameter (or message).  What this means is that we don't do this:

[WebMethod]
public bool CustomerExists(int customerID) {
  // implementation
}

By coding our web service to accept a single integer as a parameter and returning a boolean we have limited our web service, and made it very difficult to expand upon the implementation.  Instead of using an integer and a boolean in our implementation we should be using messages that wrap the integer and boolean.  Web Services are about messages so don't ignore them when developing your web services just becuase VS.NET makes it possible to ignore that fact.  Instead take a message based approach to developing your services.  Wrap your web service parameters in "messages" and return the results in a single message.

public class CustomerExistsRequest {
  int customerID;
}

public class CustomerExistsResponse {
  bool doTheyExist = false;
}

[WebMethod]
public CustomerExistsResponse CustomerExists(CustomerExistsRequest request) {
  // implementation
  return new CustomerExistsResponse(doTheyExist);
}

# RE: Web Services should only have a single "parameter"

Friday, August 13, 2004 3:01 AM by Girish    
I agree.
But, I fear the abstraction zealots who will see this as a way to allow a webmethod to accept anything and respond to everything with Strings or XmlNode.
i.e.
[WebMethod]
public XmlNode Do(XmlNode request){
return new XmlNode(some_response);
}
This will be a good candidate since this will be the most *generic* method you can have.

I really hope that people get that while hardcoding a int to a method is bad, making it so generic that any method can go through aint better. It basically another layer on top of web service.
Oh well, my pains.

# re: Web Services should only have a single "parameter"

Friday, August 13, 2004 4:02 AM by Scott Stewart    
Yeah, Clemens discussion about that on .Net Rocks caught my ear too. While it seems like such a simple point, I'm sure we all have coded a few web services that contradict this. However, it really is what extensible services are all about. Around the same day I was listening to that, I came across this (<a target="_new" href="http://blogs.msdn.com/richturner666/archive/2004/07/30/202478.aspx">http://blogs.msdn.com/richturner666/archive/2004/07/30/202478.aspx</a>) posting by Rich Turner. It basically said the same thing, but under the guise of a typeless dataset. However, the general discussion and follow-ups touch on the same point.

# re: Web Services should only have a single "parameter"

Friday, August 13, 2004 5:07 AM by Jiho Han    
I attended VSLive NY a little while ago and listened in on one of Rockford Lhotka - who seemed to be like a really funny and genuine, not to mention intelligent guy - alluded to the fact that Web Services are not really all that better than COM (I don't know much about COM btw, everyone just keeps telling me to stay away!), that is, how is a Web Service different from/better than a COM method when you have to update the interface whenever you make a change to parameters, etc. In COM world, people use to create HelloWorld(string name), then HelloWorld2(string Name, string company). In Web Services, you still do HelloWorld(string name) then HelloWorld(string name, string company)... unless you do HelloWorld(XmlDocument doc) - or HelloWorld(XmlNode) I guess.

Sure it's so generic but it's still better than having to publish a new interface every time a parameter changes. You can still validate the incoming message to your heart's content, so I don't see a big problem.

Having said that, that's not what I do right now in my web services. Maybe I should go back and change them. Oh wait, I can't! Since they are already in use, I better go create *2 methods...

By the way, I'm very much paraphrasing what Rocky said, so don't be sending him no flamers...

# re: Web Services should only have a single "parameter"

Saturday, August 14, 2004 3:26 PM by Carl Franklin    
Hey guys, thought I would chime in with a couple points. Firstly, the single parameter idea is for passing a complex type: A class, or to be completely flexible, an XML document. I don't think Clemens meant to divide up your methods to take a single int or string.

Secondly, You can't overload webmethods in .NET, so the HelloWorld(string Name) and HelloWorld(string Name, string Company) won't work.

That was a very cool episode and really got me thinking about what Web Services are good for. Transport. They don't have all the features of a tightly bound assembly, so maybe we expect too much? I know my expectations haven't been completely met yet.

# re: Web Services should only have a single "parameter"

Sunday, August 15, 2004 10:38 AM by Steve    
Hey Carl,

Thanks for dropping in. The recent episode with Clemens as well as this past weeks with Miguel were awesome. Clemens certainly has a lot of ideas and views that make me think about the way I develop software. At this point I really need to give his architectural views a test drive in a real world application. I'll be very interested to see what Proseware looks like from an architectural point of view.

What I was trying to point out in my post is exactly what your mentioning Carl. Instead of passing string's and int's as parameters to our web services we really need to be passing &quot;messages.&quot; By passing messages we allow ourselves the opportunity to extend our web services in the future. My belief is that we should be creating XSD's for our the input and output messages used with our web services. This allows us to share the schema, use xsd.exe to generate our c# &quot;message&quot; classes, and do web services the &quot;right&quot; way.

# re: Web Services should only have a single "parameter"

Monday, August 16, 2004 10:23 AM by Jiho Han    
Carl,

I think you missed a &quot;2&quot; at the end of HelloWorld there :) And I know I don't want to be passing a single string or int and wrap it in an XmlDocument. It was just a bad example I guess. I actually develop MSCRM customizations and they come with standard schemas that they MS publishes. Like accounts, contacts, addresses, etc.

Also if you want to do web services &quot;right&quot;, then you might want to check out XsdObjectGen tool from Microsoft: <a target="_new" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=89e6b1e5-f66c-4a4d-933b-46222bb01eb0&amp;DisplayLang=en">http://www.microsoft.com/downloads/details.aspx?FamilyID=89e6b1e5-f66c-4a4d-933b-46222bb01eb0&amp;DisplayLang=en</a>

It's like souped-up xsd.exe if you will. I haven't yet tested it out myself but it's being touted as the xsd.exe replacement for the near future.

Post a Comment

 
 
Prove you're not a spammer: 
6 + 6 =