Solved

MVVM WPF application

Posted on 2015-01-10
4
277 Views
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
0
Comment
Question by:shragi
  • 2
4 Comments
 
LVL 69

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.
0
 
LVL 22

Expert Comment

by:ambience
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.
0
 

Author Comment

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

Accepted Solution

by:
ambience earned 500 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 ...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now