Adventures in Ruby MVVM – Bootstrapping Ruby

July 7th, 2010

More Adventures in MVVM Shout it kick it on DotNetKicks.com

In this post, I want to discuss how I am loading the ViewModels into the View.  When I write my Views, I like to use the tools I have available to me; Visual Studio and/or Expression Blend.  This means that I want a Visual Studio project and I want to be able to use these tools to create new views quickly.  The XAML files that define my views will still be backed by the obligatory auto-generated C# code, but this is where I want my C# code to end (for these experiments).  I don’t want to write an more C# code than that (with one exception).

My ViewModels, however, will be written in Ruby.  I will use RSpec to specify these ViewModels and I will use an editor other than Visual Studio to edit the code; mostly because Visual Studio does not have any tooling support for Ruby!  At some point, my View world needs to converge with my ViewModel world. 

To support this, I have written the only C# code that I intend to write in these experiments.  It is a bootstrapper, if you will, to load Ruby ViewModels.  It looks like this:

public class ViewModelLocator : DynamicObject
{
    private readonly ScriptEngine engine;

    public ViewModelLocator()
    {
        engine = IronRuby.Ruby.CreateEngine();
        engine.Runtime.LoadAssembly(typeof(INotifyPropertyChanged).Assembly);
        engine.Runtime.LoadAssembly(typeof(ICommand).Assembly);

        LoadAllViewModels();
    }

    private void LoadAllViewModels()
    {
        Directory
            .EnumerateFiles("ViewModels", "*.rb").ToList()
            .ForEach(file => engine.ExecuteFile(file));
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (base.TryGetMember(binder, out result))
            return true;

        result = FindViewModel(binder.Name);

        return result != null;
    }

    private object FindViewModel(string name)
    {
        return engine.Execute(name + ".new");
    }
}

This ViewModelLocator class exists for one reason: to generate Ruby ViewModels.  It creates a Ruby script engine and pre-loads all the ViewModels when it is constructed.  It then sits around, waiting to create new ViewModels for the View:

<StackPanel Orientation="Horizontal" DataContext="{Binding PersonViewModel, Source={StaticResource VMLocator}}">
    <TextBlock Text="{Binding first}" Margin="5,0" />
    <TextBlock Text="{Binding last}" Margin="5,0" />
</StackPanel>

In the XAML above, I am setting the DataContext of the StackPanel to create a new PersonViewModel (defined in Ruby). The binding references a ViewModelLocator as its source that was defined in App.xaml:

<Application.Resources>
    <IronRubyMVVM:ViewModelLocator x:Key="VMLocator" />
</Application.Resources>

A dead-simple ViewModel for illustration is defined in Ruby:

require File.dirname(__FILE__)  + "/../RubyVM/ViewModelSupport"

class PersonViewModel
    include ViewModelSupport

    declare_notifiable :first, :last

    def initialize
        @first = "Brian"
        @last = "Genisio"
    end
end

You can see all the code in action from the snapshot of my project from which I wrote this post:  I will be working on this project quite a bit after this post, so if you want to see what I have been working on, the code is always available.

Adventures in Ruby MVVM – A ViewModel Base Class in Ruby

June 19th, 2010

More Adventures in MVVM Disclaimer: When it comes to Ruby, I am still a hack.  That is all. :) In my last post, I talked about how to fire events from Ruby code such that .Net code can subscribe and receive them.  I showed a simple implementation of INotifyPropertyChanged; the interface that is essential to MVVM development in WPF and Silverlight Read More...

Adventures in Ruby MVVM – Firing Events from Ruby

June 14th, 2010

More Adventures in MVVM Disclaimer: When it comes to Ruby, I am a hack.  That is all. My Experiment: Can I move over to Ruby as my primary programming language when developing WPF and Silverlight applications?  I have been playing around with what it would mean to use Ruby to write my ViewModels, which would also make it easier to incorporate models using Ruby Read More...

Adventures in MVVM – ViewModel Location and Creation

June 4th, 2010

More Adventures in MVVM In this post, I am going to explore how I prefer to attach ViewModels to my Views.  I have published the code to my ViewModelSupport project on CodePlex in case you'd like to see how it works along with some examples. Some History My approach to View-First ViewModel creation has evolved over time Read More...

Disabling Navigation Flicks in WPF

June 2nd, 2010

I am currently working on a multi-touch application using WPF.  One thing that has been irritating me with this development is an automatic navigation forward/back command that is bound to forward and backwards flicks.  Many of my touch-based interactions were being thwarted by gestures picked up by WPF as navigation Read More...

Adventures in MVVM – My ViewModel Base – Silverlight Support!

May 14th, 2010

More Adventures in MVVM EDIT: Here is why I love blogging and sharing code so much: Putting your ideas and code out into the public space always manages to add value to the ideas that were originally posted.  Case in point: Tobias Richling commented on this post with a fantastic refinement to the Silverlight dynamic property binding Read More...

Adventures in MVVM – My ViewModel Base

May 8th, 2010

More Adventures in MVVM First, I’d like to say: THIS IS NOT A NEW MVVM FRAMEWORK. I tend to believe that MVVM support code should be specific to the system you are building and the developers working on it.  I have yet to find an MVVM framework that does everything I want it to without doing too much Read More...

The Silverlightning Talks

April 16th, 2010

Tomorrow, I will be speaking in Grand Rapids at the Silverlight Firestarter.  It is a one day event intended to get people bootstrapped with Silverlight.  I will be giving the “Advanced Topics” presentation.  I have decided to run it as a series of “Lightning Talks”.  The idea is to give a lot of breadth so you know that the topic exists and move quickly between them Read More...

Goodbye XML… Hello YAML (part 2)

March 27th, 2010

Part 1 After I explained my motivation for using YAML instead of XML for my data, I got a lot of people asking me what type of tooling is available in the .Net space for consuming YAML.  In this post, I will discuss a nice tooling option as well as describe some small modifications to leverage the extremely powerful dynamic capabilities of C# 4 Read More...

Prism Slides and Demo

March 11th, 2010

I recently gave a presentation on Prism at the Ann Arbor .Net Users Group.  I have made my slides and demo available for download: Slides   Demo Some interesting links associated with prism: Composite Application Guidance Composite Application Library Codeplex Site Great 4-part video series Another video series that David Giard pointed me towards