Home-grown ORM vs. DataTables?

.net, datatable, orm

Solution

Datatable will certainly be conceptually more straight forward in working with data. And its devoid of sometimes unnatural idioms that you find in ORM. (querying a record into local memory, before updating it; joins are pointers; the key value itself is a pointer, hence, adding a record requires loading the parent record)

The big advantages for ORM are...

1) it writes the sql for you, so you dont really have to write any sql to do basic crud. Of course writing more complex statements has to be done in a less powerful sublanguage (i.e. hql)

2) The other big advantage of ORM is when you get results back, it maps it into value objects, without writing a bunch of code to map the values and handle type conversion.

If you have strong sql skills but want advantage 2 covered, i would go with ibatis

Problem

This is a simplification of the issue (there are lots of ways of doing things), but among applications that need to talk to a database I have usually seen one of two patterns: - Object-Relational Mapping (ORM), where (usually) each table in the database has a corresponding "row wrapper" class with public properties that match the columns in the table. Sometimes these classes also automagically retrieve related information, so that foreign key columns can instead be exposed and displayed as the related data (rather than just the PK values). - DataTables (and/or DataSets), where data is retrieved from the server as a DataTable and worked with in that form (even in the UI). One of the major differences between the two approaches is that ORM allows you to reference strongly-typed fields in your code like so: ``` Person bob = new Person(); bob.FirstName = "Bob"; collectionPeople.Add(bob); ``` whereas with the DataTable approach your code would be something like: ``` DataRow newrow = datatablePeople.NewRow(); newrow["FirstName"] = "Bob"; datatablePeople.Rows.Add(newrow); ``` In this case, the ORM approach benefits from compile-time checking while the DataTable approach does not. On the other hand, the DataTable (and the DataSet) are already-written data structures that do an excellent job of representing relational data directly, so code that uses them can usually be implemented more quickly. In addition, code that uses DataTables can be easily understood and modified by others; home-grown (and often COTS) ORM systems often do extra database access "under the hood" to populate foreign keys and so forth, which can create problems for the unaware. So which approach do you generally favor and why?

Original source

Related problems