Archive for November, 2019

hcOPFCodeGen

Saturday, November 30th, 2019

hcOPF was germinated around the time frame of Delphi 7 when I realized that dataset based applications of any complexity really became a maintenance nightmare over time.  I found that data was often stale and inconsistent in a database application because each view was tied to a different dataset.  There was no way to know when the dataset had to be refreshed, so unnecessary refreshes were often performed causing the applications to be very chatty with the database server.  Also the validation logic tended to be implemented in the UI forms and so over time, as the business rules changed, it became difficult to ensure consistent, correct validation.  There was no separation of concerns, UI code was intermixed with business and database logic.  Delphi RAD apps became spaghetti code or big balls of mud. Using objects to model the real world is much more intuitive.

Many developers using Java and other technologies were using ORMs so I started working on one myself and along the way borrowed ideas from other ORMs.  I wanted a framework that provided the encapsulation of data in objects, making applications easier to maintain over time.  Encapsulation has the advantage that to add a new attribute to a business object you only have to modify the class and that attribute becomes available throughout the application, wherever an instance of the class is used.  Contrast that to a dataset based application where you would have to modify practically every form and query/dataset where the new column should appear. Missing places, or not providing validation when editable was easy to do.

In 2009 I released hcOPF as an open source project on SourceForge.  hcOPF is different than modern ORMs that use PODOs.  Such frameworks use Delphi’s newer RTTI to access attributes.  Back when I started the framework the extended RTTI wasn’t available, so I created an ancestor class from which all persistable objects must descend.  To define a business object the framework requires you to create mapping MetaData to describe the object in terms of tables and columns.  An Object can be from a single table or the result of multiple tables joined together.

Without knowing the framework, it can be a little daunting to create object definitions, and even when you know the framework, creating object MetaData can be a tedious error prone affair.  I used the copy/paste/modify approach but that doesn’t take into account the datatype the data access layer uses for a column.  Different DALs can map columns to different field types so this resulted in lots of additional changes needed that were only discovered at run-time. For these reasons I have been working on a Code Generator tool; hcOPFCodeGen.

The code generator will currently scaffold an object definition, but it needs many improvements to support all the permutations in defining an object.  I am still working out all the use cases.  For now, it should help save time by generating a basic definition that can be further customized as needed. I have removed all commercial dependencies.  The code generator now only requires the JVCL and VirtualTreeView.  The code can be found here.

The beauty of Delphi is that it allows you to leverage your code investment with minimal churn.  It’s quite easy to migrate legacy code to later versions of Delphi with small incremental changes.  hcOPF can be used with Delphi 7 to Delphi Rio.  You may encounter some compilation issues because I have not employed a CI server to maintain hcOPF, but I plan to in the near future as time permits.