Coming from a long history of Windows VCL development with Delphi, I have often used data-aware controls to quickly hook up data sets to controls and grids. As I've been doing more mobile development, I've taken a look at LiveBindings and like its extensibility. So, I decided to try my hand at extending it!
The VCL's data-aware controls drew many programmers into a lazy trap of sorts that left a bad taste in the mouths of many. Serious developers tout how much better it is to write classes and use Object Relational Mapping, or ORMs, to abstract your database and separate the nuances of writing SQL from the task of writing the next great application. Indeed, there are many benefits to using these but in many smaller projects, hooking up controls directly to a dataset is so quick and convenient that when you move from your old Windows-only version of Delphi to FireMonkey, you might be tempted to complain about the productivity drop.
At first, I thought LiveBindings was a simple replacement and almost ignored it. After watching a couple of YouTube videos, though, I realized it went much further. Not only can it hook up datasets like I was familiar with but it can connect properties and classes in ways data-aware controls never could.
Two most compelling features of LiveBindings
The two features I feel make LiveBindings worth taking a deep look and using on a regular basis are:
- Prototype Data
- Custom Formatting
The TPrototypeBindSource component provides sample data at both design-time and run-time, helping you to get the look and feel of your applications right at design-time without having to run the app constantly to see how it looks. Forms or grids that are usually blank can now show sample data with a variety of field types such as names, dates, numbers, booleans, bitmaps, and even large chunks of text without any database connected. Prototype data can also be used for building distributable sample applications--just don’t hook up your live database.
If you’re using data-aware controls and turn on your database at design-time to see the data, you have to remember to turn off your data sets when you’re ready to run to avoid the dreaded "dataset left open at design-time" errors. With LiveBindings, even if it is hooked up to live data, the connections are restored at run-time AFTER the form is created giving you the chance to change the database connection before it goes live and avoid that pitfall altogether. My favorite feature of LiveBindings though, is Custom Formatting. There are many times that the data must be manipulated or parsed or combined before displaying. With LiveBindings, you can affect the output right within the
CustomFormat property of the
For example, let’s say you’re displaying a person’s name in a label. Insted of placing two labels, one that says "Name: " and the other with the actual value of the name from the dataset, you can simply prefix the value in the CustomFormat property in the LiveBindings link from the field like this:
'Name: ' + %s
where "%s" is replaced with the value being bound. You can also apply some formatting to the data, like upper-casing the name:
'Name: ' + UpperCase(%s)
There are several standard methods that come with Delphi. You can get to the current list of installed methods by selecting the
TBindingsList component and clicking on the ellipses button for the Methods property in the Object Inspector. This is a good starting point but as I started using LiveBindings and the CustomFormat property more frequently, I suddenly wanted more. Fortunately, there’s a way to write your own.
Beyond the Standard CustomFormat Methods
If you want to write your own CustomFormat methods for use in LiveBindings, follow the instructions on Embarcadero’s DocWiki. It might take a bit to get used to how it's structured but it works--and they show up in the methods list. I've found that if I want to change one, I have to uninstall the package it's in, close any forms that might use any LiveBindings, close the project and Delphi and restart, loading only the code for the package, then recompile and reinstall. Only then will the new LiveBindings CustomFormat method get properly registered and recognized.
Going through this procedure has some benefits that pay off later. I was recently getting data from a REST service and displaying the results in a ControlList. One of the values was in a JSON array and looked like this:
I just wanted to display the list of platforms, in this case, "x86, x64" as a short CSV list. So I wrote
JsonArrayValToCSV() and now I can call that method and pass it the raw JSON value.
Another time, I wanted to do some date calculations so wrote
One of the standard custom methods is
IfThen() which takes three parameters, a Boolean, and two strings. Just like Delphi's version in the
System.StrUtils unit, if the Boolean expression is True, the first string is the result, otherwise the second string is returned. I used this with another new custom method called
StrLen() which simply returns the length of the given string. This allows a custom message to be displayed if a string is a certain length, like if it's empty.
All these methods and more are on a new open source project on GitHub called LiveBindings - Beyond the standard. It comes with an example VCL application to demonstrate each method once they're installed.
I invite you to take a look, try them out, and let me know what you think. Feel free to make suggestions or add your own and submit a pull request. I'd love to see this library grow and be useful to many!