John Lam 1997 — 2026

Hi-fidelity two-way binding in RubyCLR

It feels like I’ve just implemented another RubyCLR this weekend. And in a way, I did. In earlier versions of RubyCLR, binding was one way – Ruby objects would hold onto references to CLR objects. CLR objects could interact with Ruby objects only via delegates that call Ruby blocks.

Now binding is two-way.

Ruby objects can be marshaled to the CLR, and the CLR can directly invoke methods on those objects. Furthermore, Ruby objects can fire events via delegates in CLR code. This means that I can now do data binding notifications.

Here’s a simple example:

I use ActiveRecord to read the first record from the People table:

@john = Person.find_first

Next I create a BindingSource to act as a broker between ActiveRecord and the data-aware controls in Windows Forms:

binding_source = @john.as_binding_source

The calls to create_textbox simply bind the Text property of TextBox controls to ActiveRecord fields:

create_textbox(form, 30, 10, binding_source, 'name')
create_textbox(form, 60, 10, binding_source, 'age')
create_textbox(form, 60, 120, binding_source, 'age')

Finally, I have a Button that triggers a Ruby block that increments the age property of the ActiveRecord:

change_button.click do |sender, args|
  # Notice that I'm changing the ActiveRecord object here!
  @john.age += 1
end

Click here to see the entire sample.

Behind the scenes, there is property change notification happening. ActiveRecord objects in RubyCLR also implement the new INotifyPropertyChanged. interface. This interface fires an event whenever a property in the ActiveRecord object has changed.

Once I got the two-way plumbing working, implementing INotifyPropertyChanged was ridiculously easy:

implements INotifyPropertyChanged

alias activerecord_method_missing method_missing

def method_missing(symbol, *params)
  symbol_name   = symbol.to_s
  equals_offset = symbol_name =~ /=$/

  return activerecord_method_missing(symbol, *params) unless equals_offset

  attribute_name = symbol_name[0..equals_offset - 1]

  if !self.attributes.include?(attribute_name) or params.length != 1
    return activerecord_method_missing(symbol, *params)
  end

  write_attribute(attribute_name, params.first)
  if defined?(@clr_shadow_object)
    if @clr_shadow_object.property_changed != nil
      @clr_shadow_object.property_changed.invoke(@clr_shadow_object,
        PropertyChangedEventArgs.new(attribute_name))
    end
  end
end

If you squint just a little bit, you’ll see that this code looks for a call to methods with the signature zzz= where zzz is a member of the attributes collection of the ActiveRecord object. I override the default ActiveRecord implementation (which is also accomplished via method_missing) of those methods to set the property and fire the PropertyChanged event.

So this means that changes to ActiveRecord objects done in Ruby cause things to happen transparently in the CLR. And with very few lines of meta-programming code required to make it happen.

The sound you hear right now is that of thousands of code generation folks slapping their foreheads in unison. Believe me, if you were coding up business entity objects in C#, you’d be reaching for CodeSmith right about now.

But with Ruby you don’t have to. And that’s a good thing.