Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why returning dataset or data table from WCF service is not a good practice? What are the Alternatives?

I am working on University Management System on which I am using a WCF service and in the service I am using DataTables and DataSets for getting data from database and database is sql server.

My questions are

  • Is using DataTables and Datasets "Good Practice" or "Bad Practice" ?
  • If it is bad, what is the alternative of DataTable/DataSet ?
  • If it is bad, what are the main reasons ?
like image 662
Khurram Ali Avatar asked Sep 16 '14 16:09

Khurram Ali


People also ask

How to return a DataTable using a WCF service?

To return a DataTable using a WCF service, we must do the following 3 things: In the first step we will create a table in SQL Server; after that we create a simple function to return the table from the database using a WCF service.

How to return data from a SQL Server table as DataTable object?

In this article, we will see how to return data from a SQL Server table as a DataTable object using WCF service and consume it in a Console application. First, we will create a simple WCF service that will return data of the Employees table as a DataTable object. Then we will consume this WCF service in a Console application.

How to create a WCF service application using DBO?

CREATE TABLE [dbo]. [RegistrationTable] Now click on the project and select WCF Service Application and provide a name for the service: Now click on the Ok Button. Then you will get 3 files in the Solution Explorer: The following image shows the following files:

How to change the class name of Service1 in WCF?

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. Write following code in the web Application: Now run the application. The following error will occur: This is a serialization issue with WCF.


2 Answers

Returning data sets from web services is not typically considered a “good practice”. The issues have been documented thoroughly in the following links:

http://msdn.microsoft.com/en-us/magazine/cc163751.aspx
http://www.4guysfromrolla.com/articles/051805-1.aspx
http://msdn.microsoft.com/en-us/magazine/cc188755.aspx

In summary, the biggest issues with returning DataSet objects from web services seem to involve serialization performance, non-.net interoperability. In addition, the generic, polymorphic nature of the DataSet generally high the data structure until runtime, as such, the WSDL definition does not provide a complete description of the method signature. As with any design decision, however, you need to weigh the costs vs the benefits and determine the best fit given your specific goals and constraints.

In terms of alternatives, you could consider using a generic collection (e.g. List<yourClassHere>) or maybe even consider some architecture revisions to permit the use of ODATA.

The following links provide some good background reference for returning entities via web services. http://msdn.microsoft.com/en-us/library/orm-9780596520281-01-14.aspx http://www.codeproject.com/Articles/127395/Implementing-a-WCF-Service-with-Entity-Framework http://msdn.microsoft.com/en-us/data/hh237663.aspx

like image 85
Seymour Avatar answered Sep 20 '22 23:09

Seymour


There are 3 reason for failed return type as datatable in WCF services

  • You have to specify data table name like:

    MyTable=new DataTable("tableName");
    
  • When you are adding reference on client side of WCF service select reusable dll system.data

  • Specify attribute on datatable member variable like

    [DataMember]
    public DataTable MyTable{ get; set; }
    
like image 39
Mukesh Methaniya Avatar answered Sep 18 '22 23:09

Mukesh Methaniya