What to test / implement in a viewmodel?

3

I'm trying to implement an MVVM (Model - View - Viewmodel) pattern that is related to MVP. Home Since ViewModel, for my understanding, is responsible for "passing" the data from the View layer to the Model, it was the responsibility of doing this intermediation, what else implements it? For example: I have a rule where in the sales form I have a checkbox for Delivery (Yes / No), and if it is marked, enable or show the fields related to the address, they are obligatory and suggest in it the address of the informed customer that is already previously registered in the repository. Where do I implement this control to enable / show the fields and make them mandatory? What about the suggestion of address?

    
asked by anonymous 04.07.2016 / 19:11

1 answer

2
  

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

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

The main function of ViewModel is to partially represent Model to not expose the latter. Because it can be dangerous to expose a Model entity directly, ViewModels is used to sanitize data entry and output, as well as implement specific validation logics.

  

For example: I have a rule where in the sales form I have a checkbox for Delivery (Yes / No), and if it is marked, enable or show the fields related to the address, they are obligatory and suggest the address of the informed customer which has already been previously registered in the repository.

Field validation (mandatory field, character limit, etc.) is rather enunciated by a ViewModel . The other visual behaviors (field enablement, address suggestions) are features that are part of the presentation layer, because they are events that deal with visual aspects and behavior of fields on the screen.

  

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

In summary:

  • Enable / show fields: View ;
  • Address suggestion: View ;
  • Required to fill in according to other fields on the screen: ViewModel (see also IValidatableObject ).
04.07.2016 / 20:13