LiveBinding Expression Can Produce Side Effects

I’ve been looking for a simple LiveBinding example that would forever change the way we perceive LiveBindings, and I think I found it. But let me start with a little background.

I’ve been interested in LiveBindings since RAD Studio XE2 shipped. One obvious reason is that LiveBindings is important if you want to easily bind your FireMonkey controls to traditional Delphi DataSets. But there was something else, something that I had a hard time putting my finger on.

I had a conviction, one that I shared publicly, that LiveBindings represented a fundamentally different way of doing things, and that we, as developers, just needed to see some examples of this new usage. Once we did, I thought, we would collectively come to a new way of looking at LiveBindings. These examples, however, have been elusive.

I remember David Intersimone (David I) asking me, prior to my 24 Hours of Delphi broadcast, to show LiveBindings used in a new and different way. I would have loved to, but I had yet to see such an example. I even blogged about this last month (http://caryjensen.blogspot.com/2012/01/whats-your-favorite-livebindings.html), asking readers to submit their favorite LiveBinding example that would open our eyes to the new way of doing things.

The response to that blog was interested. First of all, not one reader submitted a LiveBinding example. What I did get were some thoughtful comments about the limitations of LiveBindings, and these were similar to comments that I heard following my 24 Hours of Delphi presentation, as well as after a talk I gave at the Software Development Event in the Netherlands this past December.

Of these comments, the most common was that LiveBindings simply provide us with another way of doing something we already do, but in a more complicated way. Once again, I really felt like this opinion simply reflected the fact that we had not yet seen LiveBindings used in that way that would redefine, in our minds, what LiveBindings can do and how they can be used.

I recently wrote a white paper for Embarcadero on LiveBindings for their RAD in Action series. While doing so I had the time to reflect extensively on LiveBindings, including their limitations as they currently stand in this first release.

It was after I submitted the first draft for review that the example I am going to show came to me. In his technical edit, Jim Tierney pointed out that I failed to recognize that LiveBinding expressions could invoke methods of objects accessible in the input or output expression scope. I don’t know why I overlooked this. He had mentioned this fact in one of his CodeRage 6 talks. I had simply forgotten. (And there is a trick. The method must uses parentheses, even if it has no parameters.)

As I re-wrote that particular section of the paper I had an inspiration. To be honest, this inspiration came at 2:00am in the morning, as inspirations often do. That pretty much ruined my night’s sleep, as I could not wait to try what I now imagined a LiveBinding doing.

Here is the basic concept, and then I will show you a simple application. LiveBindings can produce side effects. That’s it.

Sure, even this notion is not unique. I’ve seen several LiveBindings that assign a value to the ControlComponent’s target property (the output expression), and that property has caused side effects (side effects being one of a property’s magical features). But my idea, one that I had not seen before, was that you could invoke in your SourceComponent’s SourceExpression (the input expression) a method that could, as part of its execution, produce a side effect.

Here is my simple example for your consideration. Take a look at this form, which is available in the code sample that accompanies my LiveBinding white paper.

Notice the Button on this form, the one whose Caption reads Close. This button, and another on another TabSheet of the PageControl, are associated with an ActionItem. This ActionItem has associated with it a BindExpression LiveBinding, which is a managed LiveBinding. Since it is a managed LiveBinding, there must be some code that triggers the expression engine to evaluate the LiveBinding expressions, and here is that code, associated with the ActionItem’s OnExecute event handler.
procedure TForm1.Value1EditChange(Sender: TObject);
begin
BindingsList1.Notify(Sender, '');
end;

Let’s now look at the Object Inspector, which shows the properties of the BindExpression LiveBinding.

The ControlComponent and the SourceComponent properties are both set to the ActionItem. The ControlExpression (the output expression) is Caption, a property of the ActionItem. The magic occurs in the input expression (SourceExpression). In this expression is a method invocation, which I’ve associated with the form (the ActionItem’s owner). This method, OpenCDS, produces a side effect, the opening and closing of a ClientDataSet, as you can see in the following implementation.

function TForm1.OpenCDS(Open: Boolean): string;
begin
if Open then
begin
ClientDataSet1.Open;
ClientDataSet1.LogChanges := False;
exit('Close');
end
else
begin

ClientDataSet1.SaveToFile;
ClientDataSet1.Close;
exit('Open');
end;
end
;

As you can see, when the user clicks the button, the expression engine is notified to evaluate the LiveBindings associated with Sender, which is the ActionItem in this case. The SourceExpression calls the OpenCDS method, which returns a new value for the Caption property of the ActionItem. This, in turn, causes the two buttons that are using this action to likewise adopt the caption. It also performs the rather simple side effect of closing or opening the ClientDataSet. However, there are really very few limits to the side effects that could have been implemented.

I am sure that more than a few people will be thinking to themselves that this is just another example of a LiveBinding that does something we can already do (open and close a ClientDataSet). For example, couldn’t we have simply used the following OnExecute event handler?

procedure TForm1.Value1EditChange(Sender: TObject);
begin
if
TActionItem(Sender).Caption = 'Open' then
begin
ClientDataSet1.Open;
ClientDataSet1.LogChanges := False;
TActionItem(Sender).Caption := 'Close';
end
else
begin

ClientDataSet1.SaveToFile;
ClientDataSet1.Close;
TActionItem(Sender).Caption := 'Open';
end;
end
;

My response to has three parts. First, the second event handler, the one that explicitly opens and closes the ClientDataSet, needs to have intimate knowledge of what operation has to be performed. By comparison, the first event handler simply notifies the expression engine that something about the Sender has changed. The first event handler has no details about the change, nor does it specify what should happen in response. The expression engine does the actual assignment based on the LiveBinding, and the LiveBinding, not code, defines what happens.

Second, not all LiveBindings require that you notify the expression engine. Many of RAD Studio’s LiveBindings, including Lists and Links, require no event handlers at all.
Third, for those LiveBindings that do require an event handler, all of them could potentially refer to this one, simple event handler, the one that calls the Notify method of the BindingsList. As a result, a form that uses LiveBindings to perform its various tasks may have zero or just one event handler. By comparison, if you performed those tasks using code, there would have to be many different event handlers, each one invoking its specific task and requiring specific knowledge about the operation they were designed to produce.

Basically what I am getting at is that this usage of a LiveBinding is profoundly different than normal event handler usage. The OnAction event handler is completely agnostic, as far as the operation that will result from its invocation. All of the behavior is defined declaratively in the LiveBinding, along with the actions defined in any methods that are invoked during the evaluation of the SourceExpression. What’s even more exciting is that in the future this type of effect might be achieved without any event handlers at all.

But please do not get me wrong. I am not advocating that we should start replacing traditional event handlers with LiveBindings. That would be completely missing the point. LiveBindings have their place, and event handlers have their place as well.

Successful use of LiveBindings requires us to look at our goals from a different perspective. LiveBindings can do things that really don’t fit into the event-driven model of traditional event handlers. Sure, in this version of RAD Studio they are somewhat limited, but that will change over time.

I have one final comment. Earlier in this posting I noted that invoking a method that produces side effects from a LiveBinding expression is similar to the power of side effects produced by property accessor methods. Actually, these two techniques are more closely related than you might think. When designing a new class, you might actually implement a side effect in a property accessor method, and a LiveBinding could then produce that side effect as a result of its assignment of data to the associated property.

On the other hand, side effects produced by property accessor methods are often associated with keeping the internals of that component consistent. By comparison, the types of side effects that you can introduce in methods invoked through LiveBindings can have a much more global impact, keeping many elements of a form, or an entire application, synchronized.

The white paper is available from Embarcadero’s Web site at http://edn.embarcadero.com/article/42076.

I am also giving a Webinar on LiveBindings in the RAD in Action series on March 14th. You can learn more about this Webinar at http://www.embarcadero.com/rad-in-action/livebindings.

I am also doing a session on LiveBindings during my Delphi Developer Days 2012 tour with Marco Cantù. Learn more at http://DelphiDeveloperDays.com.

Read More

Read More

Delphi turns 17 and my Delphi XE2 iOS Development PDF is released

First of all, congratulations to Delphi for her 17th birthday - I still remember the release back in 1995 - and my love for Delphi also came back in the names of my kids: Erik Mark Pascal (also 17, almost 18), and Natasha Louise Delphine (15). And now for the news from me: yesterday I held my Delphi XE2 iOS Development seminar in The Netherlands (for about 75 developers), and today I've released the first edition of my Delphi XE2 iOS Development courseware manual in PDF format.
Read More

EurekaLog and FireMonkey / Win64 / OSX

Clarification about EurekaLog and latest Delphi versions... Delphi XE2 has introduced a new visual framework - FireMonkey, as well as support for more platforms, including Win64 and OSX. There are constant income of questions about EurekaLog's future and possible support for these new technlogies. So I've decided to publish general explanation for everyone. EurekaLog and FireMonkey The answer is simple - EurekaLog will... never support FireMonkey, because there is nothing to support! FireMonkey is visual framework. But EurekaLog just don't use any visual frameworks - it don't use VCL and it certainly don't need to use FireMonkey. EurekaLog uses API for all visual UI. That's why you already can use EurekaLog with both VCL and FireMonkey applications. Of course, if you want to create your own custom error dialogs for EurekaLog or something like that - fine, use API, VCL, FireMonkey or whatever technology else. There is no limitations. However, EurekaLog is not related to VCL or FireMonkey. It's just exception tracer. EurekaLog and Win64 Sorry, but EurekaLog 6 will never support Win64. It was written without considering other platforms in mind. However, there are good news: EurekaLog 7 will support Win64. Right now we're working on it, even though it dind't appear in any public releases. Support for Win64 is our current top priority! It's expected that Win64 support will be released (as beta) after we ship stable release of EurekaLog 7. You can help this to happen sooner by testing EurekaLog. Preleminary estimate is that Win64 support will be basic for first few releases and then we'll add advanced features (like disassembler view). Another good news is that we want to make migration process as painless as possible. Ideally you should be able to install EurekaLog 7, open your old project, recompile it - and it should work. EurekaLog and OSX The case with OSX support is very similar to Win64. EurekaLog 6 is out of question, but support for OSX is planned for EurekaLog 7. This is our second top-priority. We want to finish Win64 first and then start going for OSX. Currently, there was no work done for OSX, but all current work is done with future OSX target in mind. EurekaLog and... Lazarus/MSVS C++ Yes, there is slight possibility for appearing EurekaLog for other IDEs like Lazarus or Microsoft Visual Studio (C++). However, currently this is considered as low-priority in far-far away future. Note: this information is highly preliminary and is suject to change. Also, there is no estimates available.
Read More

What’s Your Favorite LiveBindings Example?

LiveBindings, which were introduced in Delphi XE2, provide developers with new options for associating objects. And they are only one of a wealth of new features introduced in this groundbreaking version of Delphi. They are also a source of some confusion. One of the problems is that most of the demonstrations of LiveBindings are simple, in part because LiveBindings are so new. Another way to put this is that it's hard to think differently about object binding when we are so familiar with Delphi's existing mechanisms. As a result, most examples that I've seen so far duplicate much of what we already achieve in Delphi.But this is bound to change. I believe that once we start to see creative applications of LiveBindings, we, the collective Delphi community, will begin to think about them differently. I hope to jump start this process by collecting examples of LiveBindings that represent the way that we'll be using them in the future, and I'll publish these here. Of course, I'll give credit if you contribute so that you can bask in the gratitude of your fellow Delphi developers.So, here is my question. Do you have examples of LiveBindings that go beyond the obvious? Alternatively, have you seen an example that breaks the mold? Is so, please share. And, in case you haven't given much thought to LiveBindings, here is a short introduction.LiveBindingsLiveBindings is a general term for Delphi's new object/property binding mechanism first introduced in RAD Studio XE2. It is the only binding mechanism available to the new FireMonkey cross-platform component library (FMX), and is also available for traditional visual component library (VCL) components.At its core, LiveBindings is a mechanism for creating associations between objects and expressions. Expressions are strings that are evaluated by Delphi's new expression engine, and in the case of LiveBindings, they define what effect the expression will have on an object.While expressions are strings, they are evaluated by the expression engine at runtime, which is quite a bit different than your Delphi code, which is compiled by Delphi's compiler at compile time. As a result, expressions are different from other string types you normally encounter in Delphi code. For one thing, expression strings can define literal values using either single quotes or double quotes. In addition, the expression engine recognizes special methods that have been registered with it through Delphi's Open Tools API (OTA), and can employ custom output converters to transform data from one type to another. Another concept critical to LiveBindings is scope. In LiveBindings terminology, scope defines what is visible to the expression engine. Most LiveBindings require a control component (the object to which the expression will be applied), and in most cases a source component as well. In the case of these LiveBindings components, the control and source components are both in scope, making their properties available to the expression engine. Similarly, those custom methods that have been registered with Delphi from a design time package are also in scope, making those methods callable from your expressions.It's worth noting that while LiveBindings use expressions, expressions can be used without LiveBindings. Specifically, you can create a scope programmatically, adding to it the objects and methods you want the expression engine to evaluate, and then ask the expression engine to perform the evaluation, returning a value based on the expression string. It's an important point, as far as the expression engine is concerned, but not something that you necessarily need to think about when you are using the LiveBindings components.Do We Need LiveBindings?I recently spoke about LiveBindings during the "24 Hours of Delphi" broadcast with David Intersimone. One of the listeners asked a question about LiveBindings that I hear pretty often, though he gave a somewhat new twist. "Why do we need LiveBindings?" he asked. "After all, it appears that LiveBindings is just another way of doing what we already do using event handlers. It kind of seems like fishing poles. In older days we had cane fishing poles, and they worked just fine. The new fiberglass and graphite rods are nice, but they don't really do more than the old rods."I like the analogy a lot, because it actually highlights why LiveBindings are a positive thing. Let's take the fishing pole example. A recent television show on The History Channel called "101 Gadgets that Have Changed the World," the publishers of the magazine Popular Mechanics list the top 101 devices that have had a dramatic impact on our daily lives. And, guess what, fiberglass fishing poles made the list (at 100), beating out duct tape and being edged out by the stapler.In any case, the point is that while cane poles and fiberglass fishing rods perform the same task, they work differently, and fiberglass rods are functionally better on every level.I think we are going to be saying the same thing about LiveBindings, once we get our heads around them. Yes, you can do many things with LiveBindings that can be achieved without them, but as we get more familiar with their capabilities, I believe we will discover a whole range of features that are enabled only through LiveBindings.So, let me hear from you. Post a link to your example, or an example that you find on the Web, as a comment to this posting.
Read More

Free Delphi XE2 “Game of Memory” for iPhone available in AppStore

My first iPhone app (the "Game of Memory" written with Delphi XE2 and FireMonkey) is now available in the AppStore (for free), see http://itunes.apple.com/us/app/game-of-memory/id489076335?ls=1&mt=8 It was quite a journey from "start" to actual AppStore deployment, but fortunately, I now know all the steps, so from now on it will hopefully be easier (although probably not faster).
Read More

Advanced Transfers with Android

Thanks to everyone who attended this session at AnDevCon II in San Francisco and Dessert Code Camp in Phoenix. Here are the updated slides (a PDF) and code samples from this session. Please leave a comment if you have any questions and I’d be happy to?clarify?things, or point you in the right direction. You can also get the HTTP Telnet script I used in that session.
Read More

Google+ Delphi User Groups

A suggestion for self-curating the Delphi content on Google+! Why? Because although we all are lovable developers, we can only handle that many photos, reshares and caturday gifs!Using Google+ allows us to have a unified comment system, and a loosely coupled Delphi community where you can actually pick and chose among who you want to follow.Here is how:Step 1: Create your Google+ Delphi Page. With Google+ adding pages, we now can self-curate our content. This means that each of us can create our own Delphi page, which can be used to promote our blog posts, or our Delphi musings directly in the page stream. Note that we would need to refrain from posting caturday gifs, or reshares of the events of all world+dog on our Delphi page.Step 2: From your Delphi Page, Follow the Delphi User Group and/or Firemonkey User Group pages This will make your page visible to other Delphi users, assuming you also complete step 3.Step 3: Ensure that you show in public on your Delphi page profile that you follow the page(s) above. This because a Google+ page will NOT show you as a follower, unless you follow it in public. Step 4: Pick the pages and/or users you want to follow from the User Group pages. As people publicly add (i.e. follow with) their Delphi pages, the user groups will automatically grow the list of available pages and visible for all. Step 5: Enjoy a "spam free" Delphi circle!Step 6: Optionally, reshare your old personal Delphi posts on your Delphi page.The generic Delphi and Firemonkey user groups are just a start. Anybody can create their own topic-centric User Group page as well. If you want to help grow the Delphi community:Create your own Delphi page, and follow the User Groups!
Read More

SmartInspect for Delphi XE2

If you are a Delphi developer you likely already know that Embarcadero recently released this year’s Delphi update called Delphi XE2. Delphi XE2 comes with support for 64-bit systems and with multi-platform capabilities. We just released an updated version of our logging tool SmartInspect that supports Delphi XE2 for Windows and Windows 64-bit systems. Please note that we don’t currently have plans to support SmartInspect for Delphi for Mac OS X and other cross platform targets. As the SmartInspect logging library for Delphi makes heavy use of the Windows APIs for performance reasons, we first want to wait on how popular the Delphi cross platform capabilities will become. If Delphi for Mac OS X becomes more popular we will certainly consider supporting this environment as well. If you are a registered customer you can download the new version from our customer portal. You can also download the updated trial version to try SmartInspect for free. Just let us know in case you have any questions or feedback about the new version.
Read More