Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good design pattern for web method return values?

When coding web services, how do you structure your return values? How do you handle error conditions (expected ones and unexpected ones)? If you are returning something simple like an int, do you just return it, or embed it in a more complex object? Do all of the web methods within one service return an instance of a single class, or do you create a custom return value class for each method?

like image 979
Eric Z Beard Avatar asked Sep 02 '08 14:09

Eric Z Beard


3 Answers

I like the Request/Response object pattern, where you encapsulate your arguments into a single [Operation]Request class, which has simple public properties on it.

Something like AddCustomerRequest, which would return AddCustomerResponse.

The response can include information on the success/failure of the operation, any messages that might be used by the UI, possibly the ID of the customer that was added, for example.

Another good pattern is to make these all derive from a simple IMessage interface, where your general end-point is something like Process(params IMessage[] messages)... this way you can pass in multiple operations in the same web request.

like image 154
Ben Scheirman Avatar answered Oct 23 '22 13:10

Ben Scheirman


+1 for Ben's answer.

In addition, I suggest considering that the generic response allow for multiple error/warning items, to allow the reply to be as comprehensive and actionable as possible. (Would you want to use a compiler that stopped after the first error message, or one that told you as much as possible?)

like image 21
joel.neely Avatar answered Oct 23 '22 14:10

joel.neely


If you're using SOAP web services then SOAP faults are the standard way to return error details, where the fault messages can return whatever additional detail you like.

like image 1
Ubiguchi Avatar answered Oct 23 '22 13:10

Ubiguchi