MVVM duplicates model properties in ViewModel

It seems that there is a guideline according to which the model should not show its entities for presentation and that all necessary properties should be duplicated in the ViewModel

Example:

Product Id {get; set;} Name {get; set;} ....... ProductViewModel : ViewModelBase Id {get; set;} Name {get; set;} ....... 

Why is this required? I can understand this if Model does not implement INPC, but if so, then I find it completely unnecessary.

+6
source share
4 answers

When a view is bound to a model:

  • If a view needs to change or you have several views, a change in the Model will lead to changes in all Views associated with this Model.

  • In perspective, a View object that may not be intuitive; when you need to add properties and / or commands to an object, add them to the ViewModel and save the "original" properties in the model, or will you change the model?

Having a ViewModel provides an extra abstraction between one model and several (versions) of views

In general, this is just a guide, but remember that today it seems that this is normal if you need to change / update the application.

+7
source

Leadership, that’s what. It depends on situation. Purists will argue that the complete separation of the model in terms of allowing the model to change without changing the look.

I am only inclined to the properties of the proxy model if I need (either INPC or some specific logic, for example, the model has the names FirstName and LastName, but not the full name)

Otherwise, I get attached to the model (which is a public property in the ViewModel). If my situation changes and I need to encapsulate something, then I will reorganize when I need to.

I always try to ensure that the ViewModel is in place (even if it only displays the model), so that refactoring will be easier later.

+3
source

My question is: why do your models implement INPC? Do they need to do this?

Typically, models are simply DTOs and do not need any logic of change.

Also, if your basic INPC implementation comes from the MVVM environment, but your models exist in a common assembly, is this assembly needed to reference your MVVM infrastructure and possibly other WPF assemblies?

The script that we had is a set of common objects representing our data both on the server and on the client side. The client side was a WPF application, so it was good, but there was a service for the server side, so we do not need INPC.

+1
source

Your ViewModel is incorrect. If you already have a product type model, you can simply define something like this in your ViewModel model: public Product Product {...}

0
source

Source: https://habr.com/ru/post/919329/


All Articles