User Tools

Site Tools


blog:databinding_chaining_observablevalues_together

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
blog:databinding_chaining_observablevalues_together [2012/08/17 22:59]
djo created
blog:databinding_chaining_observablevalues_together [2014/10/20 15:44]
djo Added comment form
Line 1: Line 1:
 +====== Databinding Chaining ObservableValues together ======
 +
 +Sometimes the standard data binding primitives are perfect: data gets converted, validated, and reported nicely. ​ But sometimes one's validation requirements are more than fit nicely into a standard IValidator. ​ And sometimes the data bindingp pipeline that Binding implements for you isn't quite as flexible as one might want.
 +
 +What I realized after reading quite a bit about monads is that if we treat an IObservableValue as immutable for a given moment in time (e.g.: it only gets changed by an external actor to the system, like the user or a clock or...) then we can chain IObservable values together using a function that maps from one IObservableValue to another in much the same way one would use the map function to transform one functional collection to another. ​ Implementing this idea lets us write code like this:
 +
 +<​code>​
 +IObservableValue textFieldTarget = new WritableValue("",​ String.class); ​ // Bound to a UI field somewhere
 +IObservableValue model = new WritableValue(new Integer(1), Integer.class);​
 +
 +ValuePindingPipeline pipeline = new ValueBindingPipeline(bindingContext)
 +   ​.from(model)
 +      .map(new IntToSringConversion())
 +      .to(textFieldTarget);​
 +
 +pipeline.getReversePipeline()
 +   ​.from(textFieldTarget)
 +      .map(new IsValidIntegerValidation())
 +      .map(new ConvertToInteger())
 +      .map(new RangeCheckValidation())
 +      .map(new LessThanValidation(someOtherObservable))
 +      .to(model);
 +</​code>​
 +
 +As you can see, the resulting code is nice and explicit about what happens everywhere in the pipeline. ​ This is really similar to some [[http://​fire-change-event.blogspot.com/​|ideas that Matt Hall had]] , just expressed in a more monadic form.
 +
 +~~LINKBACK~~
 +
 +~~DISQUS~~
  
blog/databinding_chaining_observablevalues_together.txt ยท Last modified: 2014/10/20 15:44 by djo