What's wrong with binding your view to the property of the model and not the ViewModel?

I often hear that the Model must be wrapped by the ViewModel so that the View is not associated with the Model / not aware of it.

With MVC it's common to associate a view with a model ... no one complains, so what?

I am afraid to create all these wrappers and almost duplicate properties.

+2


a source to share


8 answers


In some cases you do not need, just as you do not need properties in many cases, but can get away with public fields.



But your model should reflect the domain structure and logic, while the view model reflects the structure and logic of the user interface. At least as far as I understood. The two are not necessarily the same, and each can change independently of each other.

+6


a source


You should always apply the template to your individual problem and modify it in areas where it does not apply. Just because a template implies a view model does not mean that you need a view model in all scenarios. In case your model provides all the required properties and does not require additional abstraction, the view model may not be needed.



However, in many cases, the view model will end up being very useful and much easier to maintain than adding additional properties to the model. Consider this carefully before linking directly to the model.

+2


a source


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

If you misuse the model, you can edit data in the database that you don't want to edit.

It is safer to use ViewModel.

+2


a source


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

But as said, if you are sure that all the data you need is already available in the model, go ahead and delete the ViewModel.

+2


a source


One common scenario where ViewModels come in very handy is when Enum values need to be displayed. In my opinion, the ViewModel is the perfect place to convert Enum values ​​into user-friendly views. You can even introduce a localization step in the ViewModel.

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

But I think most of the time it makes sense to use the ViewModel from the start, because it can be difficult to represent at a later stage of development.

+1


a source


No problem with binding directly to the model from your view where possible .

What you find, very quickly you are faced with a situation where you need to model what you need that is not in your Model .

Considering that you never want to mess up your Model with View problems , you have no choice but to create a ViewModel .. p>

+1


a source


It depends on the possible indicators: for example, if your model is provided by EF, it makes no sense to expose it to a view - why does the View need all the model method / properties and a lot of things (not to mention data security). But if your model is really simple and you feel like you are not going to extend / modify it a lot, and VM, there is nothing suitable for that :)

0


a source


You might also ask, "What's wrong if all the functionality of my program is included in one giant class?" On the one hand, there is nothing wrong; it can be made to work. On the other hand, this is not the case: if your program is one big ball of wire, you have to straighten it all out in order to change any of them.

Take a look at a Windows Forms programmer written by a beginner. You will find all the business logic in the Click event handlers. What's wrong with that? Don't you want this logic to execute when the user clicks the button?

This is essentially what you are proposing to do in the WPF world. Will this work? Sure. For trivial projects, this might even work well. However, you build up technical debt, and when the time comes, you will have to pay off refactoring your code into something manageable.

0


a source