Thursday, September 02, 2010

A new direction for bindings?

I'm happy to report that I've been given company approval to port the relevant components of our Flex data binding library back to Eclipse Data Binding.

I haven't started the actual port yet--there are still some concepts on the Flex side that are not a perfect match to Java and existing idioms in Eclipse Data Binding. You'll see what I mean.

To avoid conflating the port to Java with the general API I'm going to just present what the Flex API looks like.

  Bind.from(source, "foo")
.to(target, "bar");

This binding watches the property, and writes the new value to each time a change it detected. Now add some validation and conversion magic:

  Bind.from(source, "foo")
.to(target, "bar");

Here we've added several additional steps in the pipeline.

  • After changes, we first validate that the string can be converted to a number. If so the pipeline continues to the next step, and terminates otherwise.
  • Next we convert the string to a number
  • Now validate that the number is greater than or equal to zero. If so the pipeline continues to the next step, and terminates otherwise.
  • Now validate that the number is less than 10. If so the pipeline continues and the number, now verified to be in the range [0,10), is written to

Now suppose our binding is misbehaving somehow, and we want to troubleshoot. We can add logging steps to the pipeline in between the other steps so we can see exactly what is going on:

  Bind.from(source, "foo")
.log(LogEventLeven.INFO, " == {0}")
.log(LogEventLeven.INFO, "validate {0} is a number")
.log(LogEventLeven.INFO, "convert {0} to a number")
.log(LogEventLeven.INFO, "validate {0} >= 0")
.log(LogEventLeven.INFO, "validate {0} <>
.log(LogEventLeven.INFO, "set = {0}")
.to(target, "bar");

(In Flex, string formatting is done with {n} format instead of the %s syntax which Java inherited from C. The log statement passes the values in the pipeline as additional arguments which you can reference in log statements.)

These log steps are a real lifesaver for tracking down and squashing bugs in your binding code.

If you've already worked with Eclipse Data Binding you may have noticed something else: you are no longer constrained to the standard data-binding pipeline. You are free to add steps in the pipeline wherever you like and in any order you like.

Next up is two-way bindings. The bind class provides a twoWay method which connects two bindings to the other one's starting point:

Bind.from(source, "foo"),
Bind.from(target, "bar") );

is equivalent to:

  var lock:Lock = new Lock();
Bind.from(source, "foo")
.to(target, "bar");
Bind.from(target, "bar")
.to(target, "foo");

Notice that each binding has a "lock" step in the pipeline. Only one binding can hold a lock at a time. This solves the common infinite loop problem:

  • changes. binding one executes, writing the value to
  • changes. binding two executes, writing the value to
  • changes. binding one executes, writing the value to
  • ...
  • stack overflow!

Since only one binding can hold the lock at a time, this is what happens instead:

  • changes. binding one acquires the lock and executes, writing the value to
  • changes. binding two attempts to acquire the lock but it is already acquired. binding two aborts.
  • binding one releases the lock

You should never add the same lock more than once to a single binding, since that would guarantee that the binding will never run.

Two-way bindings can use validations, conversions, logging, locks etc just like regular one-way bindings (since two-way bindings are just two one-way bindings wired up to eachother):

Bind.from(person, "birthDate")
Bind.from(heightText, "text")

We usually leave out the validations in the model-to-UI bindings. It's usually only important to apply validations when you're copying data back from the UI to the model, to make sure domain constraints are satisfied, such as ensuring that a birth date occurred in the past.

And now for my favorite part: binding from multiple sources, to multiple destinations. Raise your hand if you have ever had to wire up a UI form like this:

  Is there a foo? (o) Yes  ( ) No <-- fooRadioGroup

Enter bar: ____________________ <-- barText


  1. fooRadioGroup.selectedItem is bound to (a boolean)
  2. barText.text is bound to (a string)
  3. barText must be enabled iff fooRadioGroup selection is Yes.
  4. When the user clicks "No," set to null but do not clear the text box. If the user clicks "Yes" again, set back to the contents of barText

Requirements 1 and 3 are easy:

  var fooLock:Lock = new Lock();
Bind.from(model, "foo"),
Bind.from(fooRadioGroup, "selectedItem"),
fooLock); // explicitly provide the lock, see more below

Bind.from(fooRadioGroup, "selectedItem")
.to(barText, "enabled");

Requirements 2 and 4 are kind of related to eachother. The model-to-UI binding is simple enough: just write the value straight across:

  var barLock:Lock = new Lock();
Bind.from(model, "bar")
.to(barText, "text");

However the inverse binding (UI-to-model) must also take fooRadioGroup.selectedItem into account to decide whether to write back barText.text (if Yes is selected) or null (if No is selected).

The Bind class has another trick up its sleeve:


Bind.from(fooRadioGroup, "selectedItem")

Bind.from(barText, "text")

.convert(function(foo:Boolean, bar:String):String {
return foo ? bar : null;
.to(model, "bar");

Look closely. The binding pipelines that we pass to fromAll(...) become the arguments, in the order they are provided, to the converter and validator functions further down the pipeline. The first pipeline is from fooRadioGroup.selectedItem and therefore that boolean value is the first argument to the converter. Likewise, the barText.text pipeline is provided second, so that string value becomes the second argument to the converter.

The converter takes multiple values but returns only a single value. This is where those values get coalesced into a single value that we can write to the model--in this case, a String value or null.

The outer pipeline adds a locking step on barLock, which is expected since we need to prevent infinite loops between the last two pipelines. However we are also locking on fooLock, on the first of the inner pipelines. We had a problem with our bindings overwriting values in the UI depending on the order things were initialized.

It turned out that without that lock, if a new model object was set, then the foo binding would fire first. Thus was copied to fooRadioGroup.selectedItem. But that would trigger our last binding to execute, so if the new foo value was false, then the last binding would override anything in the text box and set null on the field, before the => barText.text binding had a chance to execute!

A good rule of thumb is that any time you need to bind from multiple sources, you should make sure to create a lock to share between all the bindings to relate to the same field in the model.

Obviously there are several concepts that will have to be adapted to work elegantly with our existing APIs. Realms are a missing piece (Flex is single-threaded so we didn't even have to consider it). Also we would want to try to retrofit the existing binding classes to use this new API transparently, like we did with the transition from custom observables to custom properties.

So there you have it. This is my current vision of what Eclipse Data Binding should evolve toward.



Eric Rizzo said...

I like the interfaces you've outlined here (I think, the examples get a bit tricky to follow :-). However, I'm not sure the best path is one that treats 100% backwards compatibility as an absolute requirement. I know the platform team generally disagrees with my perspective, but I think this is a good time to clean up and avoid some mess and complexity in both API implementation by introducing a new API here. Deprecate the old one or leave it behind altogether or whatever, but trying to wedge this into the existing APIs seems like a nightmare.
Perhaps e4 is the right opportunity to make that jump.

Matthew Hall said...
This comment has been removed by the author.
Matthew Hall said...


Yeah, after I published this post, I looked over it and thought I may have gone into too much detail all at once.

Going forward I'll try to explore just a few concepts at a time, and probably include some explanatory diagrams.

As far as remaining backward compatible, given that the current binding API supports a well-known, limited set of binding pipeline steps:
* get value from source
* validate after get
* convert value
* validate after convert
* validate before set
* set value to target

These would translate easily to the new API:


The only tricky parts I can see are:
* make sure the realm behavior doesn't change, and
* enforcing the update policy e.g. UpdateValueStrategy.POLICY_UPDATE (the default) vs POLICY_CONVERT (up to validate after convert and then stop, except run the full pipeline when explicitly called) vs POLICY_NEVER (easy enough, this just means a one-way binding).

Matthew Hall said...


I can see your point on starting with a clean slate again, but I don't think it's necessarily an either-or in this case. If we decide to deprecate and retire old API we can always move it to a compatibility plugin.

When we do eventually decide to make backward breaking changes in version 2.0, there are a few things I would change:
* Realms. The cross-thread use case was compelling to the original DataBinding team, but somehow the design has gotten away from us to the point that we have to worry about realms everywhere. This should have been encapsulated into one place, in the form of an observable decorator.
* Make constructors protected, and recommend clients to get observables through factory methods. This gives us more freedom to evolve the API and to transparently return different classes depending on the context.
* Generics! It's time to turn the page.
* Update validators and converters to accept varargs.
* Change IObservableValue.getValueType() to return Class instead of Object. I'm just sure what the original justification was for this (so EMF observables could return EClass?) but it made a bunch of things just a tad too abstract and therefore not useful. Actually I think even the EMF observables just return the java.lang.Class.

Matthew Hall said...

Eric: After careful thought I think you're right that this may be a good time to turn the page and start fresh.

I've been gathering notes for the past several months and will post them soon on this blog for public comment.

Chennai Event Management Event Organizing Companies in Chennai said...

such a nice post…I liked it.. We are Event management companies in chennai and Event organizing companies in chennai. All the best for your future post..

Chiqueofficial said...

Need to know something more about it. Can you guys help me to find the best place for my event? I found one company called Event Management Companies in Delhi.

Jagna Co Kalani said...

Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java EE Online Training Students.

GET ME UP said...

We understand that even corporate companies need a refreshing break to re-energize. So here we are to cheer you up with zeal which will add up to great team building activities and make your events more fun and energetic! At Get Me Up, we have expertise in different types of team building programs. The location can be indoors or outdoors or within company premises Team Building Activity Organisers in Delhi

GET ME UP said...

We are one of the top event management companies in Delhi. Our team of dedicated employees is always there to help make your events perfect and memorable. We also take care of pre-event and post-event activities like setup, rehearsal, tear-down and move out expenses. Our team is flexible, spontaneous and always full of life, even at the time of crisis. Be it a classic, traditional or a modern-day setting, our team is there with their upbeat ideas turning an ordinary event into an extraordinary one. So call us or drop us a line to experience the magic of a masterpiece creation!

ragul ragul said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

sathish said...

It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

Selenium training in Chennai

Mounika said...

I am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.

Python Online certification training
python Training institute in Chennai
Python training institute in Bangalore

jefrin adams said...

Attractive blog thanks for sharing
power BI training course in chennai

service care said...

I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future
moto service center in chennai
motorola service center in chennai
moto service centre chennai
moto service center
motorola service center

anusha said...

AWS Training in Chennai AWS Training in Chennai in weekends.Learn AWS in just 5 weekends from BITA-Best Training Institute in Chennai.

Chris Hemsworth said...

The article is so informative. This is more helpful for our
software testing training institute
Selenium training in chennai Thanks for sharing

Tech Guy said...

For the best AWS training in bangalore, Visit:
AWS training in bangalore

tech updates said...

Good Article
devops training in bangalore
hadoop training in bangalore
iot training in bangalore
machine learning training in bangalore
uipath training in bangalore

verdure skills india pvt ltd said...

nice blog
get best placement at VSIPL

get digital marketing services
seo network point

verdure skills india pvt ltd said...

nice blog
get best placement at VSIPL

get digital marketing services
seo network point