views:

103

answers:

3

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?)

+2  A: 

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.

jamesaharvey
+2  A: 

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".

Michael Stum
is there any sense in dividing a viewmodel into receiver and presenter?
zsharp
I've got close to no experience with MVP-type patterns, but I don't see much need. As the Controller Action creates the ViewModel, the "Receiver" part should only contain some light validation anyway, and most of your Presenter logic is the actual View Page (YourView.aspx if you use the default WebForms View Engine). If you are doing heavy conditional logic within the ViewModel class, maybe you should extract it out into a separate class (if shared by multiple ViewModels). Really, depends on the situation, but usually ViewModels are VERY lightweight.
Michael Stum
+1  A: 

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.

ali62b