Reactive Programming

5 Dec

The thing that made me jump into WPF and adopt a new fundamentalist regime of XAML pure-ism was a talk by
Ivan Towlson in late August on Reactive Programming. His slides are available here: http://hestia.typepad.com/flatlander/files/thinking_in_wpf.pptx

An example of Reactive Programming as Ivan explained that we use often is the SUM calculations in Excel. When we change the inputs into that sum calculation the calculation is retained and re-executed. The counter example (ie not using a reactive style) would be where you supply the sum equation and when displaying the sum value, your equation is not retained and only the resulting value is displayed. This means updating inputs of the sum don’t affect the sum value. I suggest checking out wikipedia for a better introduction:  http://en.wikipedia.org/wiki/Reactive_programming

To become a Reactive Programmer. I have the following suggestions/tools

  • Put as much display logic into XAML as possible as it is declarative (there is a bigger learning curve doing it this way as XAML is frustrating to learn)
  • Use as much databinding as possible. To do this you should adopt a view model. This allows you to update view data in the view model which will update the view. If you do this properly you should be able to never reference a XAML item by name from non XAML code(ie C# in my case).
  • Be mindful that LINQ is a staic one way action that can be databound but would require events to refresh and would only be a one way binding. I recommed Bindable LINQ to get around this problem.  http://www.codeplex.com/bindablelinq
  • In reactive programming, when displaying view data from the view-model I am of the opinion that Databinding is good. Events are bad. I think this point is fairly obvious. The most immediate benefit is that it simplifies code by reducing the number of lines required. The second benefit that comes to mind is the ability to potentially copy/serialize/insepect a binding for use elsewhere or for storage for later use.

As software devs we should be trying out best to reuse all of our code and reactive programming provides a great mindset for doing this. Unfortunately some of the xaml tags that I use for reactive programming are lacking in Silverlight, so I am doing my projects in WPF and am hoping in the future that Silverlight supports more WPF. A great example of a reactive WPF application that wouldn’t work in silverlight is the quake example which I provided in my first blog post.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: