views:

34

answers:

2

I'm a little confused about MVVM.

I understand the concept and can see the advantages. My problem is: does the ViewModel pass data directly from the model.

For example, let's say I have a "User" model with a findByName() method. The ViewModel would call this in order to pass the relevant user details to the view.

The model would likely retrun a set of "User" objects each which has properties such as name, email address etc and may also have methods.

My question is, should the ViewModel return the set of User objects to the view, or return a restructured version of this which contains only what the view needs?

As I understand it, the "User" object in this case is part of the model layer and in MVVM the View should be dependant only on the ViewModel.

My issue with this is the ammount of seemingly redundant binding logic required in the ViewModel that would be created to restructure the output.

Passing the set of User objects directly to the View (via the ViewModel) would be far simpler.

+1  A: 

There's a little bit of redundancy, sure. However, if you implement MVVM by presenting the objects, you get to

  • format the model information for the view without polluting the model with presentation logic
  • notify the view when anything changes
  • use WPF's validation (if you're using WPF)
  • run acceptance tests from the VM level rather than the GUI if you want to
  • abstract your presentation away from any changes to the model.

That last one's important. Mostly presentation bindings nowadays are dynamic and fail silently - web pages, WPF, you name it. That means that if someone decides to rename something on the model, it will suddenly break in your GUI and you won't know.

By putting a VM between your Model and View you buffer yourself from changes like this.

If you want to go ahead and get something working with the Users as they are, I say go for it - it'll help you get fast feedback on your GUI. However, the first time those User objects don't do exactly what the View needs, or you need to notify the View of a change, or you find yourself polluting the model, or something in the binding breaks, maybe that's a good time to move to MVVM.

Lunivore
I'm not using WPF or silverlight I'm using PHP and I've reached a point with MVC where I've realised its limitations and that MVVM provides numerous advantages over direct view-model interaction.I'm not sure a lot of your points apply in this case. For instance, it's not possible to notify the view of changes to the modelMy views are currently doing something like this:<table><?$users = $this->viewModel->getUsers(); foreach ($users as $user) { ?><tr> <td><?=$user->name;?></td> <td><?=date(DATE_RFC2822, $user->registrationDate);></td></tr><? } ?></table>
Tom
I have it set up so that the view can be used with any ViewModel that has a specific interface,this view becomes reusable for any list of users and all the filtering logic has been delegated to the specific ViewModel. The View is doing any formatting of data I think my issue is the maintainability of it. If I add a field to the "User" object, before I can start using it in related views I'd need to update the related ViewModels, which seems like a maintainability nightmare. Especially considering behind the model I have a DataMapper mapping database fields to the User object.
Tom
Well, when it starts becoming a maintainability nightmare the other direction - because you can't change anything in your model because it breaks all your views - that would also be a good time to move to MVVM. Possibly a bit before then. ;)
Lunivore
A: 

Doesn't that just move the break to the ViewModels which are using the model? You'd still need to go through and update all of those.

If I renamed something (e.g. changed "surname" to "lastname") I'd expect things to break. I don't see how adding the binding in the VM layer fixes that.

Tom