This is a really exciting development in RubyCLR. I’ve taken the first steps in integrating Rails’ most excellent ActiveRecord object-relational mapping layer with Windows Forms data binding.
Here’s a screenshot of the result:
And here’s the code:
require 'winforms' include ActiveRecord Base.establish_connection(:adapter => 'sqlserver', :host => '.\SQLEXPRESS', :database => 'rubyclr_tests') class Person < Base end class MainForm def initialize form = Form.new form.Text = 'ActiveRecord and Windows Forms' grid = DataGridView.new grid.dock = DockStyle::Fill grid.data_source = Person.find_all form.controls.add(grid) @form = form end end WinFormsApp.run(MainForm)
There’s a lot of heavy lifting going on behind the scenes. The call to Person.find_all returns a Ruby Array object which I marshal to the CLR. However, my Ruby Array object isn’t any ordinary Ruby Array object. RubyCLR can implement CLR interfaces in Ruby, so my Array object also happens to implement IBindingList.
The Array contains Person objects, which all derive from ActiveRecord::Base. RubyCLR knows how to marshal ActiveRecord::Base objects so that I transparently create a CLR ‘anonymous’ type that exposes all of the Ruby ActiveRecord fields as CLR properties. I marshal by reference, so that changes in the ActiveRecord object will be propagated via IBindingList notifications to the DataGridView control.
This is an extremely important scenario to cover since a primary goal of RubyCLR is to enable the creation of rich client business applications. This is a major step towards enabling this scenario.
I just checked in the code for this feature into the rubyclr trunk. So you can play with it if you want. Keep in mind that this is all prototype code, but at least this sample works and all of the existing unit tests still pass (182 tests, 365 assertions).