What makes MVVM uniquely suited to WPF?
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. Ho开发者_StackOverflow社区wever, 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?
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.
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).
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).
I think commanding support (ICommand) in addition to great data binding capabilities makes it suitable for WPF and Silverlight.
精彩评论