views:

588

answers:

4

The Model-View-ViewModel is very popular with WPF and Silverlight. I've been using this for my most recent projects, and am a very large fan.

I understand that it's a refinement of MVP. However, I am wondering exactly what unique characteristics of WPF (and Silverlight) allow MVVM to work, and prevent (or at least make difficult) this pattern from working using other frameworks or technologies.

I know MVVM has a strong dependency on the powerful data binding technology within WPF. This is the one feature which many articles and blogs seem to mention as being the key to WPF providing the means of the strong separation of View from ViewModel. However, data binding exists in many forms in other UI frameworks. There are even projects like Truss that provide WPF-style databinding to POCO in .NET.

What features, other than data binding, make WPF and Silverlight uniquely suited to Model-View-ViewModel?

+1  A: 

I think commanding support (ICommand) in addition to great data binding capabilities makes it suitable for WPF and Silverlight.

Dzmitry Huba
+1  A: 

In short: it's the data binding.

Per the Data Binding Overview from MSDN:

If the binding has the correct settings and the data provides the proper notifications, then, when the data changes its value, the elements that are bound to the data reflect changes automatically. Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.

If you set your XAML up correctly, you only have to interact with your user interface using the viewmodel. WPF takes care of updating the UI when the viewmodel changes, and updating the viewmodel when the UI changes (ex. user input).

Ryan Michela
mmm - I don't feel that Data Binding is enough, in and of itself, even though this is the main touted feature. Things like Truss would allow MVVM for any .NET technology if it was just about data binding... (I do agree that it's an important building block, just not the only one). I was hoping for other, non-databinding, features that are critical.
Reed Copsey
+5  A: 

DataBinding, commands, control templates and XAML.

Without one of these, MVVM would be a lot harder, if not impossible. Take ASP.net for instance, it has the ASPX part (which for the sake of the example is equivalent to XAML), it has data binding, but it doesn't have commands or control templates, so MVVM is not possible there. In WinForms, we have databinding, and that's pretty much it, so not possible either.

Carlo
Why do you feel that XAML is required for MVVM?
Reed Copsey
For 2 reasons, DataBinding (in MVVM is done through XAML) and ControlTemplates. Without the latter, editing / creating our own control would be code-bound, making MVVM more difficult when working with user created controls.
Carlo
Databinding doesn't have to be done through XAML, and there's some who prefer to split it from the XAML. While creating code UI is harder than XAML, it is possible, and doesn't affect MVVM use. And I think data templates are more relevant to MVVM than control templates.
Cameron MacFarland
It does not HAVE to, but the point of MVVM is making it through XAML, so the actual code can run independently from it [XAML], to unit test, for instance. It's a lot better to not do any binding in the code side, also to not use events (use Commands instead), that way your controls are not bound to the events in your code.
Carlo
A: 

I've implemented MVVM's cousin pattern Model-View-Presenter in MFC, WinForms and even MATLAB. I agree with the original post: WPF facilitates data binding very nicely, but you can utilise the concepts in other platforms (albeit with more code).

Reading John Grossman's blog, the real distinguishing difference is that the user interface is to be written in a different language than the business logic. The ideal seems to be that the UI development is performed by "designers" not programmers.

This is the area that WPF is unique - I don't know of any other development environments that work towards this ideal.

(Mind you, I've never worked on a big enough team to warrant dedicated UI designers. I can't say whether this ideal is actually achievable).

Andrew Shepherd