What to test/implement in a viewmodel?

Asked

Viewed 141 times

3

I am trying to implement a standard MVVM (Model - View - Viewmodel) that is related to MVP.
Since Viewmodel, by my understanding, is responsible for "passing" the data from the View layer to the Model, outside the responsibility of doing this intermediation, what I implement most in it?
For example: I have a rule where in the sales form I have a checkbox for Delivery (Yes/No), and if this is marked, enable or show the fields referring to the address, be mandatory and suggest the address of the informed client who 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 question of the suggested address?

1 answer

2


Since Viewmodel, by my understanding, is responsible for "passing" the data from the View layer to the Model, outside the responsibility of doing this intermediation, what I implement most in it?

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

The main function of Viewmodel is to partially represent the Model not to expose the latter. How dangerous it can be to expose an entity of Model directly, is used Viewmodels to clean the data input and output, and 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 this is marked, enable or show the fields referring to the address, be mandatory and suggest the address of the informed client who is already previously registered in the repository.

Field validation (mandatory field, character limit, etc.) is stated by a Viewmodel. The other visual behaviors (field enabling, address suggestion) are functionalities 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 the fields and make them mandatory? What about the question of the suggested address?

In short:

  • Enable/show fields: View;
  • Suggested address: View;
  • Mandatory filling according to other screen fields: Viewmodel (see also the IValidatableObject).
  • In some cases would it not be more feasible to expose the model through Binds and put the validation rules in them? In my view, many validations are also part of the rules of business.

  • 1

    This is the default form, by the way. I use this by definition. Only when I need some more specific use behavior Viewmodels.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.