Dataset Vs custom object (Entity)

Labels:

Dataset

A dataset is a class in .Net representing the structure similar of a SQL database. It contains one or more tables. You can create typed dataset, it's a class which inherits from the base class dataset and have tables with specified name, columns with specified names and types. It's very easy to create with Visual Studio, in the server explorer window when you are connected to a database, you simply create a new dataset and you drag and drop the tables you need. Next you run a query against a database or via a table adapter and you fill this dataset, it's very easy. A dataset implements all the interfaces for databinding such as IErrorDataInfo, INotifyPropertyChanged, ... (see Databinding and error validation in WinForm).

Custom object (Entity)

A custom object is a class that your write by your own. It contains properties (generally no business code) and basic validations (see Databinding and error validation in WinForm). You run a query against a database and you map SQL columns to the properties of your custom classes.

Why to use dataset or custom object (Entity)

A dataset is very easy and is rapidly created but there are some problems for big projects :

  • A dataset is a fixed structure, if you have a SQL table person and in the future you have in this SQL table more than one type of person, a seller and a buyer you can't do inheritance with datatable.
  • If you plan to do a web service returning data, dataset is a big structure, if your table contains only one row of data with one column, the XML result of your web service contains more data for the definition of the dataset than for the data itself.
  • Also for web service, dataset is a class of the .Net framework and it doesn't exist in other platform, it is not WSI compliant, and it will be very difficult for not .Net client to use it.

Custom object doesn't have this problem :

  • A custom object is a basic class and you can do inheritance, you can create a class person and in the future create 2 classes, one Buyer and other one Seller and these classes inherit from the class Person. So you can benefit of polymorphism and you don't have maintenance problems.
  • Into web service, the XML output contains only properties which contains data and no more and the definitions is very small.
  • Custom object produces basic XML output WSI compliant if you use properties in your custom object which contains only primitive types or other custom objects which also contain properties with primitive types or custom objects ... You can also create properties defined as list, collection, ... and these properties will be transformed to XML array.

Conclusion

For small projects dataset is a good choice, you can create rapidly an application with databinding. Pay attention that if you create a web service it's only for .Net clients.

For big projects dataset can lead to maintenance, evolution problems, it'll be difficult to change an existing dataset, the only solution will be to create a new one. With custom object we can make inheritance, create interfaces, ... to have a flexible design for future changes. It takes at the beginning a little more time to start the project in comparison with dataset because you need to create your objects and implements the necessary interfaces if you want to take advantages of databinding (see Databinding and error validation in WinForm). But now with linq and linq to SQL it's more easier to work with custom objects.







1 comments:
gravatar
Anonymous said...
December 11, 2011 at 8:05 PM  

A Simple C# Dataset Tutorial

http://csharp.net-informations.com/dataset/csharp-dataset-tutorial.htm

yan

Post a Comment