Benefits of Implementing the MVVM Design Pattern

You're no doubt familiar with projects whose views have a lot of code-behind, where that code-behind pushes data into the view. To behave in this fashion, the code needs to have intimate knowledge of the view itself. However, many benefits could be gained by having the view's logic extracted away from the view and into another class. It would then be totally ignorant of the view, and simply act as a provider of data and operations to it. In other words, a better design would result from having a clean separation of concerns between a view's look and its behavior. This is what the MVVM design pattern aims to achieve.

images Note Although the implementation will be somewhat different, the MVVM design pattern does share a lot in common with the way that you design custom controls (discussed in Chapter 12). When looking at it from this perspective, a view is analogous to a control template, and a ViewModel is analogous to a control's logic.

One of the big issues with having all or much of the logic and behavior of a view written in its code-behind is that logic located there is simply not testable. Intertwining a view's look with its behavior means both have intimate knowledge of one another. This bidirectional tight coupling makes testing the view's behavior an incredibly difficult and messy, if not impossible, task.

Even if you do not write unit tests against your code (although you should strongly consider doing so) and don't consider testability a concern, implementing MVVM in your application still provides numerous other benefits:

  • It allows you to create blendable projects, enabling you to easily populate your views with data at design time, so the designer can see what the user interface looks like populated with data without needing to compile and run the project.
  • It enhances the designer/developer workflow, reducing the friction when both parties work simultaneously on the look, behavior, and logic of a view, without interfering with one another.
  • It provides a step toward structuring your project to support the implementation of the inversion of control principle and dependency injection.
  • It enables you to potentially reuse the view logic across multiple different view implementations.
  • The loose coupling enables you to potentially apply different Views to the same ViewModel. For example, you could reuse your ViewModels from your Silverlight project in a Windows Phone 7 edition, with different views applied to suit the Windows Phone 7 controls and form factor.
  • Even if you decide not to write unit tests against your project, having implemented MVVM in your project allows you to retrofit your project with unit tests at a later time without requiring too much (if any) restructuring.

As you can see, there are many potential benefits gained from implementing the MVVM design pattern in your Silverlight business applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset