Share →

I think that I have found one of the best articles on MVVM that I have ever read:

http://jmorrill.hjtcentral.com/Home/tabid/428/EntryId/432/MVVM-for-Tarded-Folks-Like-Me-or-MVVM-and-What-it-Means-to-Me.aspx

This article sums up what is in MVVM and what is outside of MVVM. Note, when I and most other people say MVVM, they really mean MVVM, Commanding, Dependency Injection + any other Patterns you need to create your application.

In WPF a lot of use is made of the Decorator and Behaviour pattern as well. The goal of all of this is to have pure separation of concerns. This is what every code behind file of every Control / Window / Page  should look like if you are engineering your WPF and Silverlight correctly:

C# – Ideal

  public partial class IdealView : UserControl
  {
      public IdealView()
      {
          InitializeComponent();
      }
  }

Figure: This is the ideal code behind for a Control / Window / Page when using MVVM.

C# – Compromise, but works

  public partial class IdealView : UserControl
  {
      public IdealView()
      {
          InitializeComponent();

          this.DataContext = new IdealViewModel();
      }
  }

Figure: This is a compromise, but the best you can do without Dependency Injection

VB.NET – Ideal

Partial Public Class ServerExplorerConnectView

End Class

Figure: This is the ideal code behind for a Control / Window / Page when using MVVM.

VB.NET – Compromise, but works

Partial Public Class ServerExplorerConnectView

    Private Sub ServerExplorerConnectView_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Me.DataContext = New ServerExplorerConnectViewModel
    End Sub

End Class

Figure: This is a compromise, but the best you can do without Dependency Injection

Technorati Tags:     
  • Michael Brown

    I would argue that you’re wrong on three levels.

    First it’s possible to create and assign your ViewModel in Xaml leaving a fully clean code behind.

    Second, even though it is possible to do this. That is not the goal of MVVM. The goal of MVVM is to separate the view logic that needs to be separated so that it can be testable, maintainable, and flexible. In many cases this does lead to a clean code-behind, but it’s not a requirement.

    Third, when we say MVVM we mean MVVM nothing more nothing less. People interchange MVVM Frameworks (which is what you describe) with the term MVVM. But the ViewModel pattern consists of the ViewModel and nothing else. There are supplementary patterns (such as eventbroker, delegatecommand and others) that work well with MVVM but MVVM does not include those patterns.

    –Mike

  • Compare Web Hosts

    hey, just finished reading, interesting stuff! do you offer some kind of email subscription so i can get more of your content

  • Rufus

    This is great content, even a peanut could learn from this, keep it up

  • Noosa Accommodation

    I’m adding you to my bookmarks, thanks for the tute.

  • Website Ratings

    Made a few good points there, so well done, have you locked on in my RSS, looking forward to more

  • meeting blog

    MVVM can be challenging, and this dummies guide has done me and doubtless many others a service.

  • Les

    Hi Martin. I don’t think MVVM is for me. A pattern to manage a pattern? Some say MVVM simplifies code, but I think it adds more complexity.

  • Free2Watch All Movies Online

    i have added you as bookmark. thanks for this article. can i susbscribe to your articles for now on?

  • Cheryl

    Awesome article! I now get MVVM! Jer – will you marry me? Our shop intends to use MVVM with aspx pages – no SL or WPF. Are we crazy?

  • JS

    Can someone tell me why a clean “code behind” is so important?

    I’m having a little trouble understanding why hundreds of lines of cryptic, unsteppable xml is something i should strive for in my programs.