Tip when code should be stored at the data class level and when at the representation level

When using classes and representations to perform CRUD functions, it is important to place the code at the right level.

What must be available at the class level?

  • All the default values.
  • All the controls.
  • All the code that performs the updates.

In summary, all the code that properly makes your entity work and handles the errors in service mode must be there.

The principles are: do not trust the client layer and do not expect that a forbidden operation cannot be called even if it has been disabled in the representation. It might be called in service mode, so you have to assume the control and to reject any operation if it is forbidden.

What can be placed at the representation level?

  • Rules that will help the user fill the page (for example, assistant mode).
  • Controls that might make the user more comfortable by sending some error cases earlier. In fact these controls must be done so the best would be to write the control code in the class and to call it in a redundant mode in the representation.