[Last Call] Learn how to a build a cloud-first strategyRegister Now


MVVM WPF application

Posted on 2015-01-10
Medium Priority
Last Modified: 2015-02-05
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
Question by:shragi
  • 2
LVL 70

Expert Comment

by:Éric Moreau
ID: 40543413
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.
LVL 22

Expert Comment

ID: 40544029
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.

Author Comment

ID: 40562194
noep not a home work question - but my manager asked me to some background on this and give a demo.
LVL 22

Accepted Solution

ambience earned 2000 total points
ID: 40563942
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 ...

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question