Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

609 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