The Basics of MVVM
While training and consulting with clients around the country, I find that many developers still have trouble grasping the concept of Model-View-View Model (MVVM) as used in Silverlight or WPF. In this blog post I thought I would show two examples side-by-side to help you learn how to move from the more traditional model of development to MVVM.Why use MVVM
The reasons why programmers are adopting MVVM or the Model View Controller (MVC) design patterns are the same reasons why programmers adopted Object Oriented Programming (OOP) almost 30 years ago: reusability, maintainability and testability. Wrapping the logic of your application into classes allows you to reuse those classes in many different applications. Maintaining logic in classes allows you to fix any bugs in just one place and any other classes using that class automatically get the fix. When you don’t use global variables in an application, testing your application becomes much simpler. Wrapping all variables and logic that operates upon those variables into one class allows you to create a set of tests to check each property and method in the class quickly and easily.
The thing to remember with MVVM and MVC is all you are doing is moving more of the logic out of the User Interface (UI) and into a class that simply has properties and methods that you will bind to the user interface. One example of the type of logic you move into a class is setting the DataContext property of a list box (in Silverlight/WPF) when you want to display a collection of objects in that list box control. Instead of setting the DataContext property directly you simply set a property in the class where you load that collection of objects. If you bind this property to the list box control then the list box will automatically redraw itself when this property changes.