O'Reilly logo

Programming WCF Services, 2nd Edition by Juval Lowy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Data Sets and Tables

One of the most common types of data contract exchanged between clients and services involves data that originates in or is destined for a database. In .NET, a common way of interacting with databases is via ADO.NET's data set and data table types. Applications can use the raw DataSet and DataTable types, or use the data access management tools in Visual Studio 2008 to generate type-safe derivatives.

The raw DataSet and DataTable types are serializable (i.e., marked with the Serializable attribute):

[Serializable]
public class DataSet : ...
{...}

[Serializable]
public class DataTable : ...
{...}

This means that you can define valid service contracts that accept or return data tables or data sets:

[DataContract]
struct Contact
{...}

[ServiceContract]
interface IContactManager
{
   [OperationContract]
   void AddContact(Contact contact);

   [OperationContract]
   void AddContacts(DataTable contacts);

   [OperationContract]
   DataTable GetContacts(  );
}

You can also use the type-safe subclasses of DataSet and DataTable in your contracts. For example, suppose in your database you have a table called Contacts that contains your contacts, with columns such as FirstName and LastName. You can use Visual Studio 2008 to generate a type-safe data set called MyDataSet that has a nested class called ContactsDataTable, as well as a type-safe row and type-safe data adapter, as shown in Example 3-10.

Example 3-10. Type-safe data set and data table

[Serializable] public partial class MyDataSet : DataSet ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required