Sunday, April 25, 2021

Delphi Form/Code Separation (CodeRage 9 2014)

I just re-watched an excellent video by David Schwartz on form/code separation. Schwartz gave this presentation during a 2014 Code Rage 9 session. 

Schwartz says the majority of Delphi Form-to-Form code he has seen uses what he calls "Back-Door" injection. Where all the data movement to and from controls is handled at the form level. This is in contrast with the “normal” use of properties to enforce encapsulation in the movement of data into and out of objects. He discusses "Constructor" injection and "Property" injection pointing out the pros and cons of both. 

I'm one of these "back-door" injection coders. I've never used properties. I'm learning how to use them. Here are my main reasons for not using them in the past:

  • Short how-two Delphi videos... "You can do all of this with no code".
  • They seem overly complicated to type.
  • Ignorance on the benefits provided.
Perhaps the IDE's Code Completion ( Ctrl + Shift + C ) would make creating properties and all the getters and setters a lot easier. I'll look into this. 

Help Wanted: If you know of any tutorials or videos specifically aimed at creating and using Properties please add them in the comments. Thank you.

Demo Code: The link to the demo code at the end of this video is broken. I have a copy of the demo code, Schwartz emailed it to me. I'm waiting for his permission to make it available as a download on this post. It does require the Konopka Signature Controls (formerly Raize components). 

I found this very interesting.

Have You Embraced Your Inner Software Plumber Yet?
David Schwartz

Here are time jumps to specific sections:

00:41 Overview:
14:35 Summary

External References:


  1. Schwartz has some interesting ideas. I haven't seen anything from him for some time. I wonder what he's up to now. I'm a big fan of "callbacks" or anonymous methods as a means of decoupling modules. One of the areas he deliberately didn't talk about involves data aware controls. It would be interesting to see how he integrates applications with data aware controls along with some of his other methods of data transfer. Specifically, I'm wondering about taking a query from a TDataSet and converting it to JSON to pass it to a selection form rather than just letting the selection form access the data directly.

    We'll probably be discussing pros and cons of all kinds of approaches long after I retire to my vegetable garden!

    Thanks for this.

    1. Milan, I'd like to see how Schwartz uses data-aware controls as well. Perhaps he'll drop in and make a comment or two.

  2. Thanks for re-posting this thought-provoking CodeRage session. I've just started rebuilding an app from the ground up and trying to decouple things better this time around - so your timing is perfect!

    1. You are welcome. I am also porting over an old app. Mine was last coded using Delphi 5.

  3. Thanks for bringing this back to life, Mike. Just an note tho: I believe there are NO dependencies on Raize Components. I think I simply forgot to remove something from the Uses clause.

    I'm reviewing the video and it needs to be about twice as long with a few more explanations. I'm not sure how anybody got anything from it. :/

    If there's much interest, I could go through it at a more leisurely pace on a Zoom session and see how that goes.

    Also, I set up a redirect to the link displayed at the end of the video should send you to a Dropbox folder with both the video and the Demo files. The link is:

  4. Thanks David and Michael. Some of this went over my head, but I got the gist of it and found it very interesting.