Previous | Home | Next |
The raw DataSet and DataTable types are serializable, marked with the Serializable attribute:
[Serializable] public class DataSet : ... {...} [Serializable] public class DataTable : ... {...}
We 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( ); }
Example:
[Serializable] public partial class MyDataSet : DataSet { public ContactsDataTable Contacts {get;} [Serializable] public partial class ContactsDataTable : DataTable,IEnumerable { public void AddContactsRow(ContactsRow row); public ContactsRow AddContactsRow(string FirstName,string LastName); //More members } public partial class ContactsRow : DataRow { public string FName {get;set;} public string LName {get;set;} //More members } //More members } public partial class ContactsTableAdapter : Component { public file MyDataSet.ContactsDataTable GetData( ); //More members }
It requires is a converter from a data row in the table to the data contract. DataTableHelper also adds some compile-time and runtime type-safety verification
Exapmle: Using DataTableHelper
[DataContract] struct Contact { [DataMember] public string FName; [DataMember] public string LNamee; } [ServiceContract] interface IContactManager { [OperationContract] Contact[] GetContacts( ); ... } class ContactManager : IContactManager { public Contact[] GetContacts( ) { ContactsTableAdapter adapter = new ContactsTableAdapter( ); MyDataSet.ContactsDataTable contactsTable = adapter.GetData( ); Converter<MyDataSet.ContactsRow,Contact> converter; Converter = delegate(MyDataSet.ContactsRow row) { Contact contact = new Contact( ); contact.FName = row.FName; contact.LNamee = row.LNamee; return contact; }; return DataTableHelper.ToArray(contactsTable,converter); } //Rest of the implementation }
Previous | Home | Next |