When DataSets are Painful

Today I was working on a legacy product that required new functionality and came across a use case where an OPF was a far easier solution than using TDataSet descendants.  The current product was implemented using ADO datasets and the new one, using hcOPF.  I needed to add the same functionality to both versions with minimal changes.

I decided to use datasets because the current product needed to be deployed before the one in development, and it used datasets.  The form I needed to implement contained a grid where users could toggle a single boolean field - a simple UI.  I thought there was no reason to add the overhead of hcOPF into the mix.  I was wrong!

The first wrinkle was that the data came from a SQL Server view and the column that was to be updated  belonged to a table added to the view by an outer join.  So an actual record in the source table may not exist.  While the view was updateable in SQL Server, ADO considered the TField ReadOnly.  I thought I would add a calculated field to the dataset, update the calculated field in the cxGrid, and use that value to call a StoredProc to update the underlying database column (it handles the problem whether the record exists or not).  Unfortunately, calculated fields are considered ReadOnly as well.  Using the OnCalcFields event was also problematic because I had to initialize the calculated field to the value of the column in the view to start with.

The solution was to use define a new ThcObjectList class with a custom Load method, and use a query to load the object list.  I also implemented a custom Write method in the ThcObject descendant used in the objectlist that checked if the original source column was Null.  If the source column was Null I knew I needed to insert a record into the source table, and if not, update the table with the value of the boolean attribute.

I’ve heard developers say they don’t like ORMs or OPFs because you have to learn how they work in order to leverage them effectively and there is always some scenario where the OPF boxes you in.  I would agree that there is a learning curve as with any component suite or library, but I don’t agree that an OPF limits you in any way.  You should be able to use a mix of an OPF with datasets, choosing whatever approach makes more sense.  It’s been my experience that an OPF provides a lot of necessary and valuable functionality that can shorten up front development time, and most importantly reduce maintenance over the lifetime of the software.

Maybe they were just using the wrong OPF…

2 Responses to “When DataSets are Painful”

  1. TPersistent.com » Blog Archive » If Imitation is the sincerest form of flattery, what is copying? Says:

    [...] TPersistent.com Without Persistence You Can’t Develop « When DataSets are Painful [...]

  2. Wordpress Themes Says:

    Amiable post and this mail helped me alot in my college assignement. Thank you seeking your information.

Leave a Reply