Phillip Trelford's Array

POKE 36879,255

MVMMC – MVVM grows a Controller

Ray Booysen does it again! The first time I saw Ray do a talk was at NxtGen in Cambridge, when he gave me a timely introduction to MVVM just as I was starting to play with WPF. MVVM (Model-View-View Model) is an architectural pattern, that improves on code behind, making it possible to unit test your UI code (View Model), a good thing. Over a year later and I’m just starting to play with Silverlight, Ray delivers another timely talk on Silverlight gotchas at the Edge UG in London, based on his real-world development experience (developing Financial applications). This time he introduces among other good things MVVMC (MVVM plus Controller). Here the controller takes responsibility for communication between the Model and View Model, leaving the View Model with the single responsibility of providing data to the View.

Somewhat shell shocked I was left with a few questions on implementation details, again Ray to the rescue via Twitter:

ptrelford Excellent @raybooysen Silverlight gotchas talk - MVVMC (C for controller), Network is on UI thread, browser connection limits #edgeug

ptrelford @raybooysen really liked Fat Controller slide, of Thomas the Tank Engine; controller takes responsiblity for marshalling threads etc #edgeug

raybooysen @ptrelford fat controller should be in all slide decks. :)

raybooysen Talk finished at @edge_ug. Think it went ok. Feel like I rambled a little.

ptrelford So with MVVMC the Model is observed by a Controller, which constructs & updates a View Model, which the (XAML) View binds to

raybooysen @ptrelford thanks for coming tonight. :)

ptrelford @raybooysen using MVVMC the Controller observes the Model & updates View Model, which observes user input and passes messages to the Model?

raybooysen @ptrelford viewmodel observes and notifies the the controller.

ptrelford @raybooysen thanks! So View Model takes a reference to the parent controller?

raybooysen @ptrelford no. Can surface the ui interaction via events. Or use rx if you're in for some fun.

ptrelford @raybooysen Cool thanks again! That makes sense, View Model exposes IObservable<T> properties. BTW your talk blew me away (hence questions)!

raybooysen @ptrelford no worries. Iobservable feels quite right in these scenarios. Since controller essentially owns the viewmodel

Finally, this was the same talk that Ray delivered at DDD Scotland, so the slides are already available here






F# operator overloads for WPF dependency properties

When creating a desktop application with WPF using F# there are a number of options:

For small desktop applications, the last option, creating WPF elements directly from F# can produce good self-contained code, but at time is a little less readable than the XAML equivalent. Lets consider placing a button bound to a command on a grid at a certain position.

The XAML fragment might look like this:

<Button Content="_1" Command="{Binding Key1Command}"
        Grid.Column="0" Grid.Row="2"/>


Code equivalent:

let button = Button(Content="_1")
button.SetBinding(Button.CommandProperty,Binding("Key1Command")) |> ignore


Code equivalent using (+) operator overloads to add the dependency properties:

Button(Content="_1") + Button.Command(Binding "Key1Command") 
    + Grid.Column 0 + Grid.Row 2 


The glue is just a couple of classes that define a dependency property and value/binding pair with a (+) operator overload that sets the value on the target WPF element.

For Dependency Property values:

type DependencyPropertyValuePair(dp:DependencyProperty,value:obj) =
    member this.Property = dp
    member this.Value = value
    static member (+) 
        (target:#UIElement,pair:DependencyPropertyValuePair) =


For Dependency Property bindings:

type DependencyPropertyBindingPair(dp:DependencyProperty,binding:BindingBase) =
    member this.Property = dp
    member this.Binding = binding
    static member (+) 
        (target:#FrameworkElement,pair:DependencyPropertyBindingPair) =
        target.SetBinding(pair.Property,pair.Binding) |> ignore


Finally relevant WPF types must be extended with helper methods (note: this could be code generated):

type Grid with
    static member Column (value:int) =
    static member Row (value:int) =


Attached is a sample calculator VS2010 project showing the mechanism in action.

Calculator (3.57 kb)

Trading on IM with F#

Last week saw the inaugural meeting of the London based F# user group – F#unctional Londoners. The group already has over 100 members! There were 2 talks Numerical Optimisation and Instant Messaging with F#, with Naoufel El Bachir giving a really good talk on Numerical Optimization and myself on Trading with Instant Messaging (IM). We had a great turn out, managing to fill the allocated room with around 50 F# enthusiasts, and there was some great discussion after both after the talks and then at a local pub.

A little positive feedback I’ve from the event :) :

loved your presentation on F#!

… that was awesome.

The slides from my Trading on IM talk will be attached to this post, and the video/podcast should be appear on the Skills Matter website. I’ll also attach the 100 lines of code for shopping checkout sample, that works with a barcode scanner, I showed at the start of my presentation.

In the talk I tried to introduce F# agents and asynchronous workflows. If you are interested in learning more please take a look at the following excellent resources:

Next week there is a very interesting talk to look forward to by Rob Pickering, author of Beginning F#, on Language Orientated Programming to target the GPU. And on the horizon we’re expecting in June to see Thomas Petricek, author (with Jon Skeet) of Real-world Functional Programming with examples in F# and C#. Finally, If you’d like to do a talk please do get in touch.

Trading on IM final.pptx (604.82 kb) (3.57 kb)