hcOPF - Using DataSets for Display Purposes

As I’ve already mentioned, I don’t think any good OPF should force the developer to choose to implement their entire UI using objects.  Sometimes it’s easier and requires less CPU time to simply use a dataset.

Today I found myself in just that situation.  I needed to add amounts owed on account for a particular reason in a dialog.  The dialog also displayed all previous amounts owed for the same reason in a read only grid.  I didn’t want to load a list of amounts owed, and then have to filter it to those owed for the reason in question, or load a separate list at some point for all amounts owed regardless of the reason.  This would have consumed more memory and CPU resources than necessary, and the benefit of the OPF is the ability to bind to the UI controls, validate the object, and persist it with minimal effort in a database independent fashion.  An added benefit of using an OPF is that because all updates & inserts are done through the objects, you don’t need to refresh them from the database unless they could become stale.  If you’re not going to update the data, there is little point in using an object list.

The challenge is that I still want to be database independant so I use the IhcQuery interface rather than creating an instance of a query for the DAL I am employing directly.  So I needed a way to attach a dataset I loaded using a SQL statement through the IhcQuery interface to a DataSource which only accepts a TDataset class.  Since Delphi 7 doesn’t provide the ability to cast an interface to the object that implements it, I did a quick google to find this.  The Delphi Magazine was a great read.  Thankfully I bought the CDs and there is still a lot of the material on the Net.  Thanks to Hallvard VassBotn, this code is now part of hcInterfaceUtils.

3 Responses to “hcOPF - Using DataSets for Display Purposes”

  1. Mason Wheeler Says:

    Hooray for Hallvard magic! The guy’s a true wizard and his work has been inspirational to me for showing some of the cool things that can be done when you understand what’s really going on under the hood of the language. I wish he was still blogging…

  2. Stefan Says:

    Remember to change PChar to PByte else it will break your code in Delphi 2009 and higher. (or use GetImplementorOfInterface from JclSysUtils)

  3. Larry Hengen Says:

    Thanks for the tip Stefan. I should probably use the JCL version. The JCL/JVCL has a lot of great stuff, but finding what you’re looking for and figuring out how to use it can be a challenge. I wish there was better documentation.

Leave a Reply