Monday, June 21, 2010

I've been planning for a while to create an F# WPF MVVM Template to add to the other templates that have been announced on this blog. A resent post by Mark Pearl provided a great simple example which helped kick me into gear and bring this plan to fruition.

To get us up to date, here are the links to the other templates that I have created:

This particular template is slightly different than the others. While the others had most or all of the code written in F#, the views or endpoints were still provided via a C# project. In contrast, this F# WPF MVVM template contains only F# projects.

What to Expect:

The code provided by this template creates an application that is loosely based on ExpenseIt (A simple expense report app. defined on this MSDN page). The following screenshot displays the produced application in action:

The view models are fairly standard. Each view model inherits from a ViewModelBase class. The ExpenseItHomeViewModel class contains most of the code. Since these are the two most interesting classes associated with view models, they will be the only two shown.

The views are similar to views used in any WPF MVVM application. The solution has three XAML files: ApplicationResources.xaml, MainWindow.xaml, and ExpenseItHome.xaml. Since ExpenseItHome.xaml is the most interesting of these three, it is provided below:

You can download the template installer here and find the full source at http://github.com/dmohl/FSharpWpfMvvmTemplate. I did run into a few limitations with having the views in an F# project. Because of these limitations, I would likely use a polyglot approach with a C# project as the view container and F# projects for the model and view model containers for solutions that are any more complex than this example. I plan to provide a template for the polyglot approach in my next blog post.

About Me

Daniel Mohl is a professional software developer/architect whose interests include understanding the intricacies of various programming languages, enterprise application architecture, and how to bridge the gap between business and technology. He is a an ASPInsider, O'Reilly author, blogger, speaker, and event organizer. You can follow him on twitter at www.twitter.com/dmohl.