tags:

views:

164

answers:

8

Hello,

I often hear a Model must be wrapped by a ViewModel that the View is not coupled to the Model/not aware of it.

With MVC it is common to bind the View to the Model... nobody complains so what ?

I am frightened of creating all that wrappers and doing nearly only duplicating property stuff.

+6  A: 

In some cases you don't need to, just as you don't need properties in many cases but can get away with public fields.

But your model should mirror the domain structure and logic, while a view model mirrors the UI structure and logic. At least, as far as I understood it. Those two are not necessarily the same and each one can change independently from the other.

Joey
this made the solution for me:"But your model should mirror the domain structure and logic, while a view model mirrors the UI structure and logic"
msfanboy
+2  A: 

You should always apply a pattern to your individual problem, and modify it in areas where it does not apply. Just because the pattern implies the usage of a view-model, doesn't mean you necessarily need a view-model in every scenario. In the case that your model exposes all of the needed properties and no further abstraction is needed, maybe a view-model is unnecessary.

However, in many cases the view-model will eventually be of great use and far easier to maintain than adding extra properties to the model. Consider this carefully before binding directly to the model.

Charlie
+2  A: 

The ViewModel is used to only pass the data that you really need to a view. When you use the model, instead of the viewmodel it is possible that you use data that came directly from the database.

When using the model the wrong way, it is possible to edit data in the database that you don't want to edit.

It is safer to use a ViewModel.

Jan
+2  A: 

One point which didn't seem to come up (directly) yet is that ViewModel can easily support data that should never even be in the model such as the text typed in the search text box or selected list items in case these values are needed in commands or further data bindings and passing them as parameters every time seems like too much trouble.

But as stated already, if you are confident that all the data you need is already available in the Model, go ahead and do away with the ViewModel.

Mikko Rantanen
A: 

One common scenario where ViewModels come in very handy is when Enum values should be displayed. In my eyes, a ViewModel is the perfect place to convert Enum values to user-friendly representations. You could even introduce a localization step in your ViewModel.

Furthermore, ViewModels can simplify some other scenarios. However, as you said, they can also duplicate a lot of code. That said, you could create a ViewModel with a Model property which allows to bind directly to the properties of the Model class. Now if you later realize that you need some conversion step, you can still add that property to the ViewModel and bind to that property.

But I think in most cases it makes sense to use a ViewModel from the beginning because it might be hard to introduce it in a later development stage.

gehho
A: 

It depends on may indicators: e.g. if your model is provided by EF there's no point in exposing it to your View - why the heck the View would need all model's method/properties and tons of stuff (not mentioning data security) But if your model is really simple and you feel, that you're not going to expand/change it much by and VM, there's nothing on the way to use it just like that :)

Piotr Justyna
A: 

There is no problem binding directly to a Model from your View where possible.

What you will find though is very quickly you run into a situation where you need to model things your view needs that aren't in your Model.

Given you never want to corrupt your Model with View concerns, you are left with no choice but to create a ViewModel.

Schneider
A: 

You might as well ask, "What's wrong with putting all the functionality of my program into one giant class?" On the one hand, there's nothing wrong; it can be made to work. On the other hand, everything is wrong: if your program is one big ball of wire, you have to straighten all of it out in order to change any of it.

Look at a Windows Forms programmer written by a beginner. You'll find all of the business logic in the buttons' Click event handlers. What's wrong with that? Don't you want that logic to be executed when the user clicks the button?

This is essentially what you're proposing doing within the world of WPF. Will it work? Sure. For trivial projects, it may even work well. You're accumulating technical debt, though, and when the time comes, you'll have to pay it off by refactoring your code into something manageable.

Robert Rossney
I did not ask why to separate code in classes doing software architecture.My question is about binding to models property instead of viewmodel...total different topic.
msfanboy
It is precisely the same topic.
Robert Rossney