• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

MVVM WPF application

Major advantages and differences between development of WPF application in traditional way vs using MVVM pattern and a simple sample MVVM WPF application to give a demo regarding the development of WPF application using MVVM pattern.

can you please help me with this
  • 2
1 Solution
Éric MoreauSenior .Net ConsultantCommented:
In theory, the major benefits of going MVVM is the complete separation of the UI and the code that is not UI related. Purist would say that a UI class like a WPF form should not contain code at all, only set elements in the XAML but this is really hard to achieve.

If you can extract everything not UI related, in theory again, you could reuse that layer (the VM) and put a totally new UI (for Windows Phone for example) without having to rewrite the business layer.
This sounds like a homework question? Please confirm that so that experts can help you better while at the same time abiding by EE policies.
shragiAuthor Commented:
noep not a home work question - but my manager asked me to some background on this and give a demo.
I assume when you say "transitional" you are referring to how it is generally done in Winforms. Winforms development is based on the MVP pattern and as such MVC, MVP or MVVM all share some core ideas, like the separation of model, view and presenter/controllers etc. For sake of reference, MVVM is a specialization of a base pattern known as the "Presentation Model."

The difference between them is in the placement of UI state, logic and synchronization code. Note that UI state, also known as application state in some discussions, is different from the domain/data. A good example is the isDirty flag that almost always finds its way into any UI application. All the patterns agree on one point, i.e. the "Model" should not contain any application state.

Whereas MVC, MVP and their variants divide the responsibility between V and P/C, MVVM comes up with an interesting concept of a hyper-Model, called the ViewModel. ViewModel is the classic "Model" and the "Application State" mixed in a unified way, exposed to the View as the model.

Since Winforms is MVP, and MVP allows for placement of UI logic in the presenter, it is but natural and perfectly OK to sprinkle code in the Form. The downside, however, is that it leads to decreased ability to unit-test that code. MVVM on the other hand allows cleaner separation of view and model/logic and the ViewModel is separately unit-tested.

ViewModels being first class OO entities can be composed of other VM, inherited from or extended and allow for greater re-usability. The same is the case with UserControls in Winforms, however, MVVM also allows for different views to be attached to the same VM. You can have a view for Mobile and another for Desktop, for example. A userControl cannot easily be used in this manner. Whether this is good or bad, depends on the context, because unlike a readily usable UserControl, you do need to write a Working View. As you compose more and more complex UI from other components the burden of the View becomes apparent. Note that MVVM frameworks provide ability to bind a View to its Model and thereby reduce this burden (but then you need an extra framework here)

One problem with MVVM is that a lot of code is need to create a ViewModel, which in some cases is just a vanilla wrapper over domain models, but you need that for data-binding to work, among other things. This is especially more relevant if you are not using a Framework that takes care of a lot of stuff. Moreover, as the underlying model becomes large and complex, it becomes even mode tedious to "Map" the underlying model to an almost exactly same graph of ViewModels.

It doesn't make sense to discuss data-binding (model synchronization) since it is equally available for MVVM and traditional styles.

My 2 cents ...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now