In the core of LiveBindings expressions of RAD Studio XE2

Posted on mar 30 agosto 2011 in ITDevCon

WARNING! I've been authorized by EMBARCADERO to write about RAD Studio XE2.

RAD Studio XE2 is full of nice and exciting features. One of the most awaited of them is the LiveBindings.

The LiveBindings is available to the VCL and the new FireMonkey framework and allows to connect a property object to another using an expression and a set of observers.\ Let's say Edit1.Text "is binded to" Person1.Name also in a bidirectional way. The "link" between a property (or a group of properties) and another property can also be very complex.

The following is an example of a complex bind expression that return a value:

``` {lang="Java"} "This is a full name: " + Trim(ToUpperCase(FirstName)) + ", " + Trim(ToUpperCase(LastName))

[I've waited LiveBindings for ages]( and now they are here!

So, let's look a deep inside to the core of livebindings expression evaluator,\
the TBindingExpression.

The TBindingExpression is an abstract class that allows to evaluate an expression. But, what's an expression? An expression is a string that return, or not return, a value. You could see an expression as a little function or procedure.\
To explain the concept, I've build a simple expression interpreter using an XE2 beta version.

It is very simple but shows the power of expression engine.\
This is the scriptengine while evaluates a simple arithmetic expression.

[![](/images/2011/08/lb01-300x183.png "Simple arithmetic expression"){.alignnone .size-medium .wp-image-332 width="300" height="183"}](/images/2011/08/lb01.png)

The expression engine is not only a "static" evaluator. Using the powerful Delphi RTTI introduced in Delphi 2010, you can also allows the expression to "read" and "call" property and method of your Delphi objects!!

So, now, some code is needed.

[![](/images/2011/08/lb02-300x243.png "The ScriptInterpreter code"){.alignnone .size-medium .wp-image-333 width="300" height="243"}](/images/2011/08/lb02.png)

As you can see, you can "register" some association between a real object and an alias in the expression. So, if my object are declared as the following:

[![](/images/2011/08/lb03-300x190.png "The classes used by the scriptengine"){.alignnone .size-medium .wp-image-334 width="300" height="190"}](/images/2011/08/lb03.png)

I can use a expression as the following:

[![](/images/2011/08/lb04-300x183.png "Reading properties "){.alignnone .size-medium .wp-image-335 width="300" height="183"}](/images/2011/08/lb04.png)

Going further, you can call methods in your expression!

So, if you have methods declared as following:

[![](/images/2011/08/lb05-300x204.png "Classes methods"){.alignnone .size-medium .wp-image-336 width="300" height="204"}](/images/2011/08/lb05.png)

You can use the following expression:

[![](/images/2011/08/lb06-300x184.png "AskForAValue"){.alignnone .size-medium .wp-image-337 width="300" height="184"}](/images/2011/08/lb06.png)

Obviously, when you link a property to another you should not use dialogs in the expressions, but this feature is VERY powerful.\
You can create different expression and use them as a custom calculator for specific business rules. The expressions are strings, so you can store them in a file or in a database and use them as needed. The expression engine is not a complete scripting language, but it can be used (and abused) in a very broad range of situations.

For complex business rules, I hate the classic chain

``` {lang="Java"}

so I usually use a DomainModel that use datasets only to read data.

The LiveBinding allows me to use (for complex business rules) the following chain

{lang="Java"} Data->ORM->DataObjects->Bindings->VisualControls

This is only an introduction to the LiveBingind engine. ASAP I'll post other articles about it.

I'll talk about the livebinding engine at the ITDevCon2011 conference. Will be you there?

RAD Studio XE2 will be officially presented all over the world during the “RADStudio XE2 World Tour”.\ You can find the list of all launch events in the RADStudio XE2 World Tour page.

I’ll be a presenter at 3 launch events in Italy and United Arab Emitates.\ These are the events where I’ll be (click to register):

  • 19th Dubai, United Arab Emirates
  • 21nd Milan, Italy
  • 22nd Rome, Italy

Stay tuned.