After being obsessed with WPF for so many years, I can’t just forget about it. Even though my focus is now on iOS development, I still think that WPF is an awesome platform. That’s why I wrote an iPhone app named Master WPF. It contains 500 questions, spread across 28 topics, that I painstakingly wrote, organized, and proofread until my eyes bled. The questions will help any WPF developer sharpen their skills.

It’s for WPF noobs, gurus, and everyone in between.

Master WPF on your iPhone or iPod Touch

You can download Master WPF for free on your iPhone or iPod Touch, running iOS 5 or greater. The app comes with 15 free questions so that you can try it out. If you decide that you want to master WPF with my app, you can make a small in-app purchase to unlock all 500 questions.

Guten tag! If you happen to live in Germany and would like to read my ‘Advanced MVVM’ book on your Amazon Kindle device, you are in luck. Amazon just made my book available in Deutschland. Note: the book has not been translated to German.

On a side note, I am shocked at how many copies of my book have sold. Over the past year, I’ve sold many thousands of copies and received a ton of great feedback, mostly positive. It has definitely been a great learning experience for me!

If you are curious about the Model-View-ViewModel pattern, or would like to have handy reference material at your fingertips, look no further. My good friend Karl Shifflett has just released an amazingly comprehensive set of MVVM training material, called “In the Box.” Why is it called that? Because all of the guidance is displayed in Visual Studio! If the topic you’re reading references some code or XAML file, you simply click on the link and that file opens. Pretty slick!

I just got some good news from Amazon. Folks in the UK can now get my book, Advanced MVVM, from the Kindle store. If you buy my book from Amazon’s Kindle store, you can read it on a variety of devices: the Kindle e-book reader, iPhone, iPad, your PC, and others.

Also, I have some other exciting news to share. Team Mole has been very hard at work, creating Mole for Visual Studio 2010, the second generation of our popular debugger visualizer. We’re getting close to the finish line. We expect to have our product online, and ready to be purchased, within the next few weeks. Mole 2010 has been in the works since February. It’s turning out to be an amazing tool…we’re all very, very proud of it. We can’t wait to release it! 😀

This morning I went to one of Seattle’s numerous coffeehouses to get breakfast. While waiting in line, the two guys in front of me (looked like coworkers) were talking, and I overheard them use the term “ViewModel.” When their conversation came to a lull, I politely asked if they’re talking about MVVM. The following conversation ensued, between one of the guys and myself:

Guy: “Yes, we are actually.”

Me: “Oh cool, that’s a great pattern. Always an interesting topic.”

Guy: “We use it all the time.”

Me: “What do you like about it?”

Guy: “I like that it’s based on dependency injection. That makes it really easy to test ViewModels.”

Me: “What do you mean that it’s based on DI?”

Guy: “I mean, it’s based on DI. Have you ever actually used the pattern?” [Guy2 now smirks pretentiously]

Me: “Yes, I’ve used it. But, to me, it’s based on Models, Views, and ViewModels. That’s it, no DI.”

Guy: “Well it sounds like you don’t really get it then. It’s complicated, so you need to read a lot about it.”

Me: “Oh, that’s good to know. What do you suggest I read to learn about it?”

Guy: “Start with Josh Smith’s MVVM article in MSDN Magazine.”

I kid you not. That is literally what the guy told me. I burst out laughing so loud that I felt embarrassed and had to leave. The guy must have thought I was psychotic!

It’s worth noting that my MVVM article on MSDN never once mentions dependency injection, inversion of control, or any other dependency inversion technique. That’s because dependency inversion has nothing to do with MVVM.

Craig Shoemaker recently interviewed me for an episode of his Polymorphic Podcast. We touched on a quite few interesting topics, ranging from the merit of the code-behind file, to implementing Undo functionality, to the role of MVVM frameworks. You can check it out here:

It’s strange how common application architecture concerns are often ignored when it comes to supporting design-time data. By “design-time data” I am referring to pre-canned/fake data that is shown in Views while they are being displayed and edited in Expression Blend, or the Visual Studio visual design surface (a.k.a. Cider). There are two commonly seen approaches to supporting design-time data, each of which has severe drawbacks.

The most commonly seen approach is to use the d:DataContext and d:DesignInstance settings, which assign a designer-only data context for a View. The d:DataContext setting is ignored at run-time. This approach might seem great because it appears to keep design-time concerns consolidated in the View layer, which is the application layer in which “design-time” applies most directly. Nothing could be further from the truth. While having support for design-time data settings in the View layer might be great for early UI prototyping, it quickly becomes problematic as the rest of the application layers fall into place. The main problem with d:DataContext is that you now have to maintain two separate data contexts: the real one and the design one. Often people create entirely separate classes (or the designer tools generate dynamic classes) that mimic the real data objects and ViewModel objects that are used at run-time. This introduces a grotesque amount of duplication in the code base, just for the sake of showing some fake data in Blend. Having duplicated classes and extra settings in XAML just for the sake of design-time data strikes me as patently absurd. Have fun maintaining that application over the years…

Another common approach to creating design-time data is to keep the Views unaware of being in design-time, and to have their ViewModel objects handle it for them. This can easily lead to ViewModel classes that are littered with conditional logic that does one thing at design-time and something different at run-time. For example, a ViewModel that exposes a collection of Foo objects might issue a network call to get Foos at runtime, but just new up a bunch of Foos at design-time. Complicating ViewModels with repetitive conditional logic has a distinctly rotten code smell. If a ViewModel is a Model of a View, and a View should not be smart enough to know about “design-time” then, by extension, neither should its Model (i.e. the ViewModel). There has to be a better way!

Let’s take a step back and return to basics. In a layered application architecture, you separate the responsibilities and concerns into various layers. The View layer is responsible for showing things. In MVVM, the ViewModel layer is responsible for maintaining the logical state of Views, and processing user interactions. So far, we have not mentioned anything about data access. That’s the job of a layer below the ViewModel, perhaps called the Model layer or the DataAccess layer. If the application needs data, it goes to the DataAccess layer to get it. Design-time data is still data. It, too, should come from the DataAccess layer. Everything above the DataAccess layer, such as ViewModels and Views, should not be responsible for making decisions about how data is accessed: including design-time data.

In an application that I’m working on these days, I created an interface that the ViewModel objects use to access data and save data. It’s a modest sized application, so only one interface was needed so far. In a larger system you could have as many interfaces as you need to meet your data access requirements. The data access interface is implemented by three classes: one for run-time data access, one for design-time data access, and another for unit test-time data access. I use dependency inversion (specifically, the Service Locator pattern) to supply an implementation of the data access interface to the ViewModels. When the Views and ViewModels are created and start living their life, they are blissfully unaware of the run-time context in which they exist. When running my unit tests, I provide the data access implementation with whatever data I want it to return to the ViewModel that invokes it. The design-time implementation loads up some fake data from disk and passes it back into the ViewModels, which then bubbles it up to the Views on the design surface. Of course, at run-time the “real” data access implementation is used to retrieve the actual data processed by the application.

To me, the primary benefit of this approach is that almost the entire system has no concept of “run-time” vs. “design-time” vs. “test-time.” The only place in the code base that is aware of the run-time context is the tiny bit of code that determines which implementation of the data access interface to make available to the ViewModels.