0

This comment to an answer from John Saunders, a very high-rep member of Stack Overflow, says that returning an anonymous type from a web service is a bad practice.

Assuming I have the luxury of not caring about graceful degradation of my website in the face of users with Javascript disabled, why is it bad practice to have a service method, that exists only to be called via Ajax, return an anonymous object?

It seems like such a waste to whip up a concrete type, whose sole purpose is to hold my data for a few microseconds before asp.net serializes it into JSON.

asked Sep 6, 2011 at 14:29
2
  • Found this questions while trying to figure out why my ASMX tries to use XML serialization even if I forced JSON both on the ScriptMethod attribute and in the ajax call, and thus it won't serialize my anonymous object because it does not have a parameterless constructor. Have you found a solution to this? Commented Dec 13, 2011 at 17:07
  • @MatteoMosca - that should work. There's probably a problem in your code - just post a new question and I'm sure someone can help. But yeah, returning <anonymous> should work fine, and be serialized into json without a problem - this question was about best practices. Commented Dec 13, 2011 at 17:14

1 Answer 1

2

My comment was specific to SOAP web services. Consumers of SOAP web services are accustomed to working with strongly-typed data, so returning an object of anonymous type via an object return type will force them to parse the XML, which they aren't accustomed to doing.

However, for clients which do not expect (or understand) strongly-typed data, my comment does not apply. If they are already accustomed to parsing XML, or evaling JSON, then returning an anonymous object won't make that worse.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.