Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a ViewModel have its own logic?

When assigning ViewModel fields, should the domain objects be passed directly to the ViewModel objects which will then determine how to present the data, or should another class like a service be assigning data from the Model to the ViewModel?

also:

EDIT:is there any sense in dividing a viewmodel into receiver and presenter? (instead of binding only certain fields on update?)

like image 593
zsharp Avatar asked Jan 29 '10 02:01

zsharp


2 Answers

Usually the Controller Action takes the business objects and puts whatever is needed by the viewmodel.

If you have a business object that contains the fields Name, Address, Id and the View should only display the Name, then the ViewModel only has a field "Name", and the controller action populates it. The ViewModel should know nothing about your Business Classes, it should only know about the stuff that it needs to display.

The main/only logic is then "Display Logic", that is stuff like "if TotalAmount is negative, display it with CSS Class negativeNumber".

like image 182
Michael Stum Avatar answered Sep 20 '22 05:09

Michael Stum


For the sake of SoC if that logic relates to View they safe to be in ViewModel or even in View itself but if they relate to Business or Program put them on Model and Controller respectively.

like image 38
ali62b Avatar answered Sep 22 '22 05:09

ali62b