Reputation: 13756
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?)
Upvotes: 6
Views: 3535
Reputation: 5403
For the sake of separation of concerns (SoC), if that logic relates to View, then it is safe to be in ViewModel or even in View itself. However, if it relates to Business or Program, then put it in Controller.
Upvotes: 4
Reputation: 180924
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".
Upvotes: 7
Reputation: 14281
In my experience I've used services to map the Model to the ViewModel. I don't put logic in my ViewModels.
As an aside, it's probably worth your while to check out AutoMapper to assist you with the mapping. Definitely helps cut down on writing repetitive mapping logic.
Upvotes: 3