Data ETL: Adding Custom LiveBindings To Your Apps
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. I’ve been using Delphi’s LiveBindings more and more lately and think they’re more useful than many people give them credit. I’ve started an open-source library of CustomFormat methods on GitHub: https://t.co/yUds77hYIQ. I’d be interested to hear what people think. — David Cornelius (@delphidave) May 26, 2021 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 datasource / 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: 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; begin Result := MakeInvokable(function(Args: TArray): IValue var InputDate: TDateTime; RoundDigits: Integer; MinutesSince: Double; begin CheckArgs(Args, InputDate, RoundDigits); MinutesSince := RoundTo(TTimeSpan.Subtract(Now, InputDate).TotalMinutes, RoundDigits); Result := TValueWrapper.Create(MinutesSince); end); end; function MakeMinutesSinceMethod: IInvokable; begin Result := MakeInvokable(function(Args: TArray<IValue>): IValue var InputDate: TDateTime; RoundDigits: Integer; MinutesSince: Double; begin CheckArgs(Args, InputDate, RoundDigits); MinutesSince := RoundTo(TTimeSpan.Subtract(Now, InputDate).TotalMinutes, RoundDigits); Result := TValueWrapper.Create(MinutesSince); end); end; That code means you can add a LiveBinding custom expression MinutesSince(MyDateTimeDBField, 2) to output the […]
