In the recent Data ETL focused TCoffeeAndCode episode there was some excellent discussion around the use of FireDAC to extract and manipulate data in various ways.
The replay, in case you missed it, the replay video is linked below.
Data ETL done the easy way – thanks to a tweet!
We also – among many other things – talked about the benefits of LiveBindings. Literally ten minutes before we were due to start the webinar I came across a Tweet from newly-minted Embarcadero MVP David Cornelius where he mentioned that he had been experimenting with RAD Studio’s LiveBindings.
Well, that was perfectly-timed! David goes on in his blog post to describe a little about LiveBindings and his personal journey where he went, not unlike myself, from a feeling of lukewarm complacency toward them to an appreciation of the finer points of their use.
LiveBindings are more powerful than they appear
When I first heard of LiveBindings they seemed, to me, to be some kind of variation on the old
datafield kind of relationship found in older versions of Delphi and the venerable Visual Basic. They looked like we should link the controls on the screen to a field (or fields) of a database table, albeit with a prettier graphical relationship editor.
Once you start to scratch the surface of LiveBindings, if you’re anything like me, LiveBindings will start to gradually reveal themselves and you’ll find there is a LOT more power going on than simple point-this-field to this-control kind of relationships. Linking controls to classes and internal code data structures is one of those Programmer Super Power RAD Studio features I keep telling people is the reason why a fairly lazy coder like myself can actually be scarily productive. It’s not me, it’s RAD Studio and, in my case at least, Delphi.
Data ETL with LiveBinding Expressions
There are some other great articles around which describe LiveBindings from initial beginnings through to more advanced topics:
- Quickly Learn How Easy It Is To Bind DataSets To Visual Controls In Delphi With LiveBindings (embarcadero.com)
- Search results for “livebindings” – RAD Studio API Documentation (embarcadero.com)
- Delphi Binding Expressions, Properties and Components (marcocantu.com)
But David Cornelius embraced the idea of LiveBindings and went a little further!
How to extend LiveBindings with custom binding expressions
David has produced a set of code, available on GitHub, which adds some custom expressions in the LiveBindings editor.
The repository contains a set of units that can be included in your apps which then enhance the available LiveBinding custom expressions to include things like automatically calculating the years since the date contained in the database field (or record or class – it’s a LiveBinding, don’t forget).
LiveBindings Custom Expression
Here’s a few lines of example code from David’s repo:
function MakeMinutesSinceMethod: IInvokable;
Result := MakeInvokable(function(Args: TArray<IValue>): IValue
CheckArgs(Args, InputDate, RoundDigits);
MinutesSince := RoundTo(TTimeSpan.Subtract(Now, InputDate).TotalMinutes, RoundDigits);
Result := TValueWrapper.Create(MinutesSince);
That code means you can add a LiveBinding custom expression
MinutesSince(MyDateTimeDBField, 2) to output the value of “MyDateTimeDBField” as the number of minutes since the date and time contained in that field. Very nice.
Alternate ways of calculating dates and time in LiveBindings
David uses some pretty efficient code in his custom expression methods. If we include the
System.DateUtils unit in our own code we could change and/or extend David’s functions to include almost any of the date/time calculations contained in that unit, where appropriate. For example we could create a function called “IsAM” and link it to the
IsAM function in the DateUtils unit and get it to show some special text if the linked datetime field was, or was not, in the morning or afternoon.
Are RAD Studio LiveBindings efficient?
Of course, the more functions you add and the more units you include the more your code will balloon out so use them sparingly. You also need to make sure that your custom expressions are as tight and as fast as possible because they are linking to and working on database fields or structures and you can, of course, expect a slow custom LiveBinding to impact overall inefficiency if you’re dealing with thousands or maybe tens of thousands of records.
Where to read more on custom LiveBindings
You definitely have to head over to David Cornelius’ excellent blog article about the custom live bindings: https://corneliusconcepts.tech/beyond-standard-livebindings
You can also go to his GitHub page and clone the repo directly from there: https://github.com/corneliusdavid/LiveBindingsBeyond
At the time of writing David doesn’t yet appear in the Embarcadero MVP directory but he will soon. I can’t wait to see more of what he comes up with!
RAD Studio Delphi makes data extraction, transformation and loading – Data ETL – fast, efficient and easy. Why not download a trial copy today?