The ViewModel Distilled
August 10, 2009 § Leave a comment
A MVVM starter project for your consumption.
For the past few weeks, I have spending my evenings learning Silverlight 3. It has been fun & insightful. I had experience developing in Adobe Flex, so aside from the syntax it was a smooth transition. One of the goals I had when I started was understanding how to leverage the ViewModel pattern in my applications. ViewModel architecturally structures your code in a manner similar to the Model-View-Controller & Model-View-Presenter patterns. The benefit of using ViewModel is that it capitalizes on the context (client applications) and features (data binding) of rich internet applications. However learning the pattern was a bit frustrating. There was a shortage of programmatic examples for implementing the ViewModel pattern in Silverlight. I found a few for Silverlight 2, and fewer for version 3.
There were a few excellent & helpful resources:
Model-View-ViewModel In Silverlight 2 Apps
ViewModel Pattern in Silverlight using Behaviors
Using Model – View – ViewModel with Silverlight
These sources greatly helped my conceptual understanding & implementation of the ViewModel pattern. However, each implemented the pattern in slightly different ways using different requirements. Without a standardized implementation, the learning curve was longer than I expected.
I got through it though (yay!). I managed to create a couple different applications using ViewModel. After writing the third application though I was getting tired of recreating the same classes and directory structures. What I needed was a template. Something I could open when I needed it and start coding. Most of the online sources I reviewed were very rich solutions so they did not lend themselves as ‘starter’ projects. Others did not provide a complete project I could reuse. So I opted to create my own ‘distilled’ version of the ViewModel pattern in Silverlight 3 using Microsoft’s Visual Web Developer 2008 Express Edition. The application itself is very basic (it only displays an employee’s name). But using this project as a template allows me to shave a few minutes of writing whenever I create a new ViewModel-based Silverlight application.
Feel free to download it and tweak it to your desires:
http://cid-a537fc3c292692a7.skydrive.live.com/embedrow.aspx/.Public/MVVM|_Simple.zip
Some time after I created the template project, I stumbled upon the Silverlight Model-View-ViewModel Toolkit. It is an open source project designed to solve the problems I was facing when I was learning ViewModel. The toolkit is in its infancy, but worth the look if you are interested in learning about implementing ViewModel in Silverlight.