r/wp7dev • u/miguelos • Mar 03 '12
Is MVVM necessary?
As someone coming from a ASP.NET/Windows Form background, I feel natural to use code-behind.
I started learning Windows Phone using code-behind, but I soon realized people were using the MVVM pattern. I tried MVVM Light and Caliburn, but they don't feel natural to me. I have to use all kinds of hack and workaround to do things I could very simply using code-behind.
Is it possible (or encouraged) to do MVVM without a framework? What do company use? What does Microsoft use to build its applications? Why isn't there an official MVVM framework?
I just don't understand why it should be so complicated, and I'm trying to decide what I will use (and what I won't) for my next application.
1
u/mariusg Mar 05 '12
One of the problems with MVVM is that in anything but very simple apps you end up with pretty large ViewModels (which eat more memory than going the MVP/codebehind route). And this can become a problem on the phone.
1
3
u/aijoe Mar 03 '12
If you are making a flashlight application or something of equivalent simplicity MVVM isn't recommended and probably adds too much overhead. People used to say the same thing about OOP in C++ when the language was much newer because they didn't want to think about encapsulation and believed that it was harder to understand and design oo systems. The majority got over it though. Likewise after using MVVM for 4 years now I think in the pattern naturally when creating apps.
0
Mar 04 '12
MVVM is nothing like OOP. OOP is a fundemental principal of programming languages like C++. MVVM is merely one of many possible design patterns that you could use for a XAML-based application. It's a starting point, not a guiding principal.
And even then, there isn't one true MVVM pattern. There are actually many competing interpertations, many of which are incompatible with each other and some even violate traditional OOP design principals regarding how to define and use abstract interfaces.
3
u/aijoe Mar 04 '12
I didn't say MVVM is the same as OOP. I was clear that what was said about it was that it was hard to grasp by some and the harder to grasp concepts and apply them in a domain than C. I was involved in such conversations on usenet more than 20 years ago. This is a fact. Its also a fact that people say the same thing about MVC/MVVM/etc. That was my only point in that regard.
2
Mar 04 '12
Is it possible (or encouraged) to do MVVM without a framework?
Silverlight and XAML data binding is your MVVM framework. Everything else on top of it is purely optional.
What do company use?
SYNTAX ERROR
What does Microsoft use to build its applications?
Thrid-party contractors, who in turn use whatever they want so long as the applicaiton works.
I tried MVVM Light and Caliburn, but they don't feel natural to me.
That's because they are not totally not necessary. And depending on how you structure your application, they can cause far more harm than good.
In fact, Caliburn micro stated as a demo of how to build your own application-specific framework. But people didn't understand the message and demanded that it get turned into something they could just download. End result: people are fighting with the tool instead of using it.
1
u/mariusg Mar 05 '12
You need at least a sane way to implement INotifyPropertyChange....
1
Mar 06 '12
True, but the vast majority of them require you to hard code strings, expensive stack walking, or tedious lambda expressions.
The only real winners are Notify Property Weaver or .NET 4.5's CallerMemberName attribute.
2
u/mhd420 Mar 04 '12
I think the real key to using MVVM is to use it where it makes sense. There's always going to be purists who want to have a IMessageBox service injected into their view model by a IOC container, but its a lot of boilerplate for no real gain.
Just use MVVM in a way that provides benefits over a codebehind - in particular databinding. If you have a list of objects that never change, then just bind directly to the properties exposed on a class, then in the view model add those objects into an ObservableCollection. If you have things that are going to change or require two-way binding, then inherit from the INotifyChanged class and use an Observable Property. There's plenty of examples on the internet. Dependency properties are for the birds and only relevant if you're making a control. By leveraging two-way binding you'll get the biggest advantage out of MVVM and XAML, and going back to changed event handlers in the code-behind feels like the stone age.
For the things that are difficult to do with MVVM like showing message boxes or navigation, just shove them into the code-behind or reference the method directly in the view model. Those code won't be 'pure' but who gives a shit, you'll get stuff finished without 3 layers of injection and interfaces.
It's like any design pattern really, use it for what it's good for.