Thursday 17 October 2013

Basics: Reasons to move from DataTables to Generic Collections

I think, these days no community member writes or speaks about using DataTables and DataSets for data operations. But, there are a number of real projects built using them and many developers still feel happy when they use them in their projects. Sometimes it is not easy to completely replace DataTables with typed generic lists, particularly in bulky projects. But now is the right time to move as future developers may not even learn about DataTables :).

Generic collections have a number of advantages over DataTables. One cannot imagine a day without generic collections once he/she gets to know how beneficial they are. Following is a list of reasons to move from DataTables to collections that I could think now:
  1. DataTable stores boxed objects, one needs to unbox value when needed. This adds overhead on the runtime environment. Whereas, values in generic collections are strongly typed, so no boxing involved.
  2. Unboxing happens at runtime, so is the type checking. If there is a mismatch between types of source and target, it leads to a runtime exception. This may lead to a number of issues while using DataTables. In case of collections, as the types are checked at the compile time, such type mismatches are caught during compilation.
  3. .NET languages got a very nice support for creating collections, like object initializer and collection initializer. We don’t have such features for DataTables.
  4. LINQ queries can be used on both DataTables as well as collections. But experience of writing the queries on generic collections is better because of intellisense support provided by Visual Studio.
  5. DataTables are framework specific; we often see issues with serializing and de-serializing them in web services. Generic collections are easier to serialize and de-serialize, so they can be easily used in any service and consumed from a client written in any language.
  6. ORMs are becoming increasingly popular and they use generic collections for all data operations.
  7. Mocking DataTables in unit tests is a pain, as it involves creating the structure of the table wherever needed. But a generic collection needs a class defined just once.

These are my opinions on preferring collections over DataTables. Any feedback is welcome.

Happy coding!

3 comments:

  1. Unfortunately many controls (grids in particular) don't play as nicely with collections as they do DataTables. (Sorting, filtering etc)

    ReplyDelete
    Replies
    1. Have you checked the Model binding in ASP.NET web forms? It works pretty well with generic IQueryable collections.

      Delete
  2. ASP.NET controls like grids work quite well with ObjectDataSource connected to your business layer methods which return generic collections. This allows for a clean N-Tier application structure. I was fortunate to never use DataTables and start clean. 5 years ago we based our project on article by Imar Spaanjaars and never regretted. Here's its latest variant:
    http://imar.spaanjaars.com/573/aspnet-n-layered-applications-introduction-part-1

    ReplyDelete

Note: only a member of this blog may post a comment.