c# – What to test/implement in a viewmodel?

Question:

I'm trying to implement an MVVM pattern (Model – View – Viewmodel) which is a relative of MVP.
Since the ViewModel, by my understanding, is responsible for "passing" the data from the View layer to the Model, apart from the responsibility for doing this intermediation, what else do I implement in it?
For example: I have a rule where on the sales form I have a checkbox for Delivery (Yes/No), and if this is checked, enable or show the fields referring to the address, which are mandatory and suggest the address of the informed customer who is already there. previously registered in the repository.
Where do I implement this enable/show fields and make them mandatory control? What about the address suggestion question?

Answer:

Since the ViewModel, by my understanding, is responsible for "passing" the data from the View layer to the Model, apart from the responsibility for doing this intermediation, what else do I implement in it?

The reverse is also true: passing data from the Model to the View .

The main function of the ViewModel is to partially represent the Model so as not to expose the latter. Since it can be dangerous to expose a Model entity directly, ViewModels are used to sanitize the input and output of data, in addition to implementing specific validation logic.

For example: I have a rule where on the sales form I have a checkbox for Delivery (Yes/No), and if this is checked, enable or show the fields referring to the address, which are mandatory and suggest the address of the informed customer who is already there. previously registered in the repository.

Field validations (mandatory field, character limit, etc.) are instead stated by a ViewModel . The other visual behaviors (field enabling, address suggestion) are functionalities that are part of the presentation layer, as they are events that deal with visual aspects and the behavior of fields on the screen.

Where do I implement this enable/show fields and make them mandatory control? What about the address suggestion question?

In summary:

  • Enable/show fields: View ;
  • Address suggestion: View ;
  • Obligation to fill according to other screen fields: ViewModel (see also IValidatableObject ).
Scroll to Top