msfanboy
msfanboy

Reputation: 5291

Whats so bad about binding your View to Property of a Model and NOT ViewModel?

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.

Upvotes: 2

Views: 352

Answers (8)

Robert Rossney
Robert Rossney

Reputation: 96722

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.

Upvotes: 0

Jack Ukleja
Jack Ukleja

Reputation: 13511

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.

Upvotes: 1

Piotr Justyna
Piotr Justyna

Reputation: 4966

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 :)

Upvotes: 0

gehho
gehho

Reputation: 9238

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.

Upvotes: 1

Mikko Rantanen
Mikko Rantanen

Reputation: 8084

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.

Upvotes: 2

Jan
Jan

Reputation: 10068

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.

Upvotes: 2

Charlie
Charlie

Reputation: 15247

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.

Upvotes: 2

Joey
Joey

Reputation: 354506

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.

Upvotes: 6

Related Questions