Are there exceptions to no code in view model?

63 Views Asked by At

The general rule of thumb is that there should be no android specific code in view models to allow for easy testing.

I need to pass a bundle with lots of metadata to the view model to filter and then return to the view. The logic is pretty dense, it would best for the view model to handle this.

Are there any exceptions to this rule, such as using Bundle or other non-Activity or Fragment android classes? Or is there a suggestion work around?

1

There are 1 best solutions below

0
On

This will probably get flagged, because it's very opinion based. There is no ultimate authority, as such, decreeing these things.

I personally have worked on some massive MVVM projects, both WPF and Xamarin, containing hundreds of thousands of lines of source code without a single line of code-behind anywhere. I can also honestly say that in the 10+ years I've been working with WPF I've never seen a single case where a problem wasn't solved elegantly by strict adherence to the paradigm. That's not to say some solutions weren't more verbose or convoluted...many most definitely were...but they were all elegant and, above all, robust.

Not everybody is as much an MVVM purist as I am. Josh Smith, one of the most respected authorities on WPF/MVVM, jokingly wrote in his book Advanced MVVM: "Others insist that there should never be a single line of code in the code-behind, based on the strange notion that code does not belong in the code-behind. What an odd bunch!"

The problem with any technology such as WPF is that the distinction between application layers is blurred at the best of times. We've all seen inexperienced WPF developers who, when faced with the power of XAML, start trying to cram as much logic into the view layer as possible. And then there are people who solve the code-behind problem by simply moving it into converters and behaviors, not realizing that these are really just a slightly more abstracted form of code-behind. (Not that I'm saying they shouldn't be used, I use them myself all the time, just keep in mind they are still view logic!)

So yeah, it's complicated. At the end of the day it's up to you, as the developer, to make the best and most sensible decisions based on the requirements of your project.