gjok
gjok

Reputation: 195

Return datatable or xml from webservice?

I am designing a C# webservice, but I'm not sure what the best practice is for outputting a single customer record ?

It currently returns a DataTable (and it works very well), but is this the 'normal' approach, or should I use a DataRow, a DataSet or simply construct a plain old XML output.

Upvotes: 1

Views: 3418

Answers (3)

John Saunders
John Saunders

Reputation: 161773

Web services are, ideally, platform-agnostic. In general, it's not a best practice to return DataTable or other types that are specific to .NET. Your clients may not be able to deal with them.

If you're just returning a single customer, then just return it. If you're returning a list of customers, then just return the list:

[OperationContract]
public Customer GetCustomer(int id);

[OperationContract]
public IEnumerable<Customer> GetAllCustomers();

WCF will serialize this properly, in a way that permits your clients to handle the return.

Upvotes: 1

Everything will depends on the client that you are expecting, some return types are not available on all devices, for that i try to always return custom class.

But if your end works well with datatables, use it i have see datatables as a return type on a lot of production code

Upvotes: 0

Convictional
Convictional

Reputation: 21

From my experience with C# webservices, returning a DataTable to your front end is fairly standard practice.

If you're returning a single record, XML is not needed.

Upvotes: 2

Related Questions