I am creating a web service in Visual Studio 2010. If it returns a DataSet (strongly typed), can someone from another platform access this开发者_如何学JAVA endpoint and use the XML as needed, or will it look like garbage to them?
What are my alternatives?
EDIT: what if I returned a DataTable? I have read microsoft recommending against this in favor of DataSet for some reason
Yes, cross-platform coders will still be able to read the XML, but it is not the best option. Scott Hanselman says:
Returning DataSets from WebServices is the Spawn of Satan and Represents All That Is Truly Evil in the World
Here is the XML produced for simple DataSet containing 1 table, with 2 columns and two records:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table1>
<Column1>hello</Column1>
<Column2>world</Column2>
</Table1>
<Table1>
<Column1>hello</Column1>
<Column2>greg</Column2>
</Table1>
</NewDataSet>
Your cross-platform coders will need to parse the XML, which can become very cumbersome, especially if the data is complex.
DataSets are really about representing the Database - in a well designed, layered application, they should only be used in the Data Access Layer. They should then be morphed into strongly-typed domain/model objects, which know nothing about databases.
A much better option is to use custom objects. There is an example for how to do this on Ryan Farleys blog.
The main advantages of using custom objects are:
1. They are fully interoperable.
2. They are strongly typed.
3. The consumer is not responsible for parsing/deserializing XML.
4. Separation of concerns between data storage and consumer.
Side-note:
I should point out that web-services are becoming out-dated. If you're creating a new web service you really should use WCF. It takes some learning but it's a much richer and more flexible platform.
精彩评论