In MVC, should a View View Model?

If the view knows the model:

enter image description here

or not:

enter image description here

?

+5
source share
3 answers

Programmers often abbreviate this and make the presentation specific to the model. For example, if you are in a CRM application, the model may have a firstName field; the view then assumes that the model object specified by it has a field firstName and shows that in the appropriate place.

This, of course, cannot be reused. If you create a view to display a data table, it does not matter which model field is displayed in the column. It should simply handle the display and formatting of tabular data in a general way. But if your presentation is a web page that is tailored to specific data, it might be okay.

Thus, you need to decide, in each case, whether you want the view to know about the specific data that it shows, or if you want it to be a reusable component.

In any case, any changes in the model data should always occur through the controller. The controller is responsible for enforcing your business logic, and this is not possible if something else bypasses it.

+2
source

No, the model and view interact through the controller.

I mean, you can know each other, but it will cause a hard connection and it will be difficult to expand the functionality of the application.

+1
source

By right, a model, a basic business object, should not be displayed directly in the view. Usually we use a ViewModel, a presentation-specific model that is derived from one or more models.

0
source

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


All Articles