The MVVM Twilight Zone

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.

Happy coding…

57 Responses to The MVVM Twilight Zone

  1. peteohanlon says:

    Now hopefully they read your blog as well.;-)

  2. Josh Smith says:

    I certainly hope so! 🙂

  3. Glenn Block says:

    IoC is a technique that plays well with applying patterns like ViewModel, but agree completely that it is not a fundamental of the pattern. Rather it is a common approach when using it.

  4. Josh Smith says:

    I agree, Glenn. MVVM and IoC can definitely be used together very effectively, but they are orthogonal practices. Thanks for pointing that out.

    Josh

  5. Ivan Danilov says:

    Thanks for sharing this! As I see it is a lot of fun for you to visit such coffeehouses 🙂

  6. Matin says:

    Really funny story, thanks for sharing 😀

  7. Patrick Klug says:

    This is hilarious! – Good on you for just laughing it off 🙂

  8. Josh Smith says:

    Hey, thanks guys. Sometimes life presents you with a situation that is so stupid, there’s no choice but to laugh. 🙂

  9. jake says:

    Didn’t you write a longish schpiel about why you never covered DI in your book? Why didn’t you suggest that excellent book on lulu by jo-something or other? By the way i got my hardcopy by post.

  10. Vasco says:

    Albeit their misunderstanding, I wonder if they could be referring to prism..

  11. tobi says:

    “The guy must have thought I was psychotic!” maybe you are… 😉

  12. Awesome story 🙂 Your MSDN article is a classic, and when I first started looking around the MVVM Light Toolkit I thought: “Oh, hello!” (of course *afterwards* I made the connection).

  13. Greg Duncan says:

    LOL! Classic…

  14. Josh Smith says:

    Thanks everyone. 🙂

    @jake – Yes, I did write about why my book ‘Advanced MVVM’ doesn’t cover DI. It’s in my response to Ward Bell’s review of the book. Thanks for picking up a copy of my book, by the way. I hope you enjoy it!

  15. judahgabriel says:

    Hahah! Awesome.

  16. judahgabriel says:

    (…awesomely bad, on the coworkers’ part?)

  17. wesaday says:

    Hilarious! Well you deserve it for sneaking around incognito…. 🙂

  18. Josh Smith says:

    You’re right. I need to start wearing a “Hello, my name is…” sticker. 🙂

  19. Chris says:

    PRICELESS!!! 🙂

  20. Santosh Kulkarni says:

    Haha!! Hilarious Post Josh !

  21. Tian says:

    Funny! 🙂

    BTW the link in the right hand pane to your WPF library on CodeProject is broken.

  22. shopen says:

    Reminds me of that scene in annie hall with marshal mcluhan. http://www.youtube.com/watch?v=OpIYz8tfGjY

  23. mike greenway says:

    I think you should listen to them and read your book!

  24. Tom Jorgenson says:

    Isaac Asimov once attended a class on literature and he proudly noted that one of his books came up for analysis. But he was very frustrated when his classmates incorrectly (he felt) described the meanings behind the story line differently than what he had written.

    He approached the instructor with this complaint and was told “What makes you think you know the meaning behind your stories – you’re only the author?”

    He thought about it and ended up agreeing with the teacher. The stories’ hidden meanings, and values, were not necessarily what he’d put into them – but rather what people got out of them.

    That isn’t to say that MVVM provides DI, though. It doesn’t.

  25. Youngjae says:

    Hello Josh.
    Sorry to bother you.
    I cannot find your contact, so I write on here.

    I bought your MVVM book at Lulu but thers’s an error to download. The support team says that I shall contact to you and receive the pdf directly.

    my Lulu order# is 4258871 and already paypal checked.

    check out your book status and the link https://support.lulu.com/Question.jsp?id=012315d04d709c575389b3fce7ef3b59&
    for tracking my Lulu help inquery.

  26. Really nice, Josh. Enjoyed a lot.

  27. Scott McCain says:

    My first inclination after reading this entry was “wait a minute, MVVM (and MVP) are based on IoC, right? While DI is a specialization of IoC, that doesn’t make MVVM and MVP automatically DI but doesn’t it follow IoC principles? That was until I read a little more into IoC (I read Martin Fowlers article) and read some more on MVP and now I see the light. I think the distinction to made here is that IoC (and consequently DI) is made to decouple implementation details. While at first glance it seems MVVM and MVP do that it’s not really implementation that is abstracted since a window is implemented by the OS there is nothing really to abstract. What is getting abstracted however, is the representation of state. It was when I realized what was actually being decoupled and abstracted that I realized it’s not truly IoC and therefore can’t be DI.

    It’s true that DI can be very powerful when used in conjuction with MVVM (and I plan on doing just that by using MEF to create a plugin UI architecture), but you can still get away with it without DI.

  28. Michaël says:

    I have just finished to read your article on MVVM and I fall on this.
    Very hilarious !

  29. Josh,

    I take it you did not want to embarrass them by introducing yourself. I have been developing in Prism V2.2 using M-V-VM since reading your article in MSDN Magazine. I really like your post in March about using the IFocusMover interface. It has been a huge benefit for those of us working with error validation and focus control. Do you plan to upgrade this class to handle the StringFormat clause of Binding? If not could you provide some thoughts on how to add that functionality myself?

  30. Josh Smith says:

    Hey Chuck,

    Please elaborate on the StringFormat issue, and I’ll get back to you when I get a chance (currently digging myself out after being on vacation for a week).

    Thanks,
    Josh

  31. Josh,

    Since .Net 3.5 sp 1 databinding in XAML has included a clause for StringFormat:

    If I try to use this with jas:FocusBinding instead of Binding, I get an IntelliSense and compiler error: “The property ‘StringFormat’ was not found in type ‘FocusBinding’.”

    I also find that if I get sloppy and do not include the Path= keyword on my binding then the code will not compile.

    Any insight you can provide will be welcome.

  32. Sorry I tried to include XAML code example and it did not come through.

    “”

  33. Sorry All,

    Third times a charm

    TextBox x:Name=”CoPhoneNumber”
    Text=”{Binding Path=Phone,
    Mode=OneTime, StringFormat={}{0:###-###-####}}” />

  34. Sorry All,

    Third times a charm (hopefully)

    TextBox x:Name=”CoPhoneNumber”
    Text=”{Binding Path=Phone,
    Mode=OneWay,
    StringFormat={}{0:###-###-####}}”

  35. Josh Smith says:

    Chuck,

    I accidentally used an older version of BindingDecoratorBase, which does not have the StringFormat property. Just go in and add that property to that class, and it should work fine.

    Josh

  36. Dave says:

    Now this happens when you shave off that beard 😀
    People don’t reconize you anylonger…

  37. Don Burnett says:

    Haha funniest story ever. Maybe they were confusing documentation with the enterprise library and unity stuff who knows. They didn’t recognize you though? wow..

  38. Colby says:

    Absoluty one of the funniest software stories I have ever heard. Josh insted of leaving you should of showed them some ID & recommended some reading materials.

  39. Dundler says:

    I am new to WPF. I need a lot of reading to do. May be this question will clear out my mind later but I am just curious to know if there is a specific scenario for which MVVM need to be considered. Or is it best to use all the time, even for smaller applications?

    Thanks.

  40. Larry Parker says:

    This is on par with the McLuhan scene in Annie Hall.

    “Boy, if life were only like this.” 🙂

  41. gioVhan says:

    This is really funny. I like the catch 🙂

  42. Chalres says:

    i was noticing, that in your famous MVVM article on MSDN that you write your end regions like so:
    #endregion //Fields

    I write mine like this:
    #endregion Fields

    is there any reason for the comments that make it easier?

    I didn’t know where else to contact you to ask this. So i posted here, since it was related.

  43. Josh Smith says:

    Charles,

    The colorization of the commented text makes it easier for me to see where a region ends.

    Josh

  44. Daniel Varrin says:

    Hi,

    I’ve ordered an Adobe Digital copy of the “Advanced MVVM” book. I’ve received the order confirmation, but I did not receive any link where I can get the book.

    Best regards,
    Daniel

  45. Visu says:

    Hey Josh,

    I think at least you will come to know what’s cooking behind you in the MVVM world. 🙂

    Thanks,

    Visu.

  46. Kai says:

    That’s why we need your picture posted!

  47. Jimmy says:

    Josh, I am new to WPF…..recently graduated….
    Can you suggest something best, using which i can do ground running….

  48. Jimmy says:

    Actually i want to work with you if you can give me a chance…. as i am also looking for the job nowadays….

  49. Josh Smith says:

    Jimmy,

    You can’t tell someone you just graduated and don’t know WPF, and then ask for a job. That’s not how it works.

    If you want to get started with WPF, I suggest you first know C# or VB.NET, the .NET Framework, and how to use Visual Studio. Once you are grounded in those core topics, then perhaps buy a book or two about WPF, and read some articles/blog posts on the Web. The most important thing is to keep writing programs and experimenting with new ideas, classes, etc.

    Josh

  50. Chalres says:

    Jimmy,
    I recommend the book ‘Visual C# Step by Step’. It not only goes over c#, but will teach you how to use visual studio all while giving you exposure to WPF, and other important .NET features.

    Charles

  51. Sam Rabeeh says:

    Josh, curious if those two gents ever came back to your site and saw this post, or commented to you. I imagine that would be like eating cotton.

  52. robin says:

    one of the funniest things i’ve read this year.

  53. vishal says:

    Hello Josh,

    I have started reading your blogs and articles recently.I want to request you following.
    Can you please uncover the things for dependency properties like, there should be mappings between source and target in dependency system somewhere. How these mappings are managed, which type of data structure is used for these mappings. References Will be present in these mappings then how Garbage collector is working for these references?

  54. Josh Fischer says:

    Hilarious, absolutely hilarious. You’ll have to forgive me, but I can’t help wondering if your fibbing a little; the story is just too perfect! To show them up, I would have said “that’s a good idea, let me ask Josh Smith what he thinks.” I then would have proceeded to have a two sided conversation with myself. 😉
    Thanks for sharing, I’m still laughing.

  55. Sumit says:

    Hi Josh,
    Went through your article/code for creating new customer. I have one question as While creating new customer you are creating new instance of CustomerViewModel, Is new instance of View being created every time or the same instance of view is being used by multiple viewmodel instances ? I was actaully trying to add a usercontrol in CustomerView but looks like every time a create a new customer only one instance of UserControl is being created. Can you please help.

    Thanks

  56. Siva says:

    Hilarious 😀

  57. Brett Hickenbottom says:

    Hilarious. My guess is that they’re using an MVVM (or MVVM-ish) framework that incorporates DI on some fundamental level. Few people see MVVM from the eyes of a purist.

    I would have stuck around, introduced myself, and spent some time trying to figure out where the disconnect was. Those situations are invaluable learning opportunities.