You have probably noticed that as our use of WPF grows more sophisticated, our controls start getting large, and often filled with complex logic. You are not alone in noticing this trend. Microsoft architects Ken Cooper and Ted Peters also struggled with the idea, and introduced a new software architectural pattern to help alleviate it: Model-View-ViewModel. This approach splits the user interface code into two classes: the View (the XAML + codebehind), and a ViewModel, which applies any logic needed to format the data from the model object into a form more easily bound and consumed by the view.
There are several benefits to this pattern:
- Complex logic is kept out of the View classes, allowing them to focus on the task of presentation
- Presentation logic is kept out of the Model classes, allowing them ot focus on the task of data management and allowing them to be easily re-used for other views
- Presentation logic is gathered in the ViewModel class, where it can be easily tested
Essentially, this pattern is an application of the Single-Responsibility Principle (that each class in your project should bear a single responsibility).