views:

49

answers:

4

Hello guys,

I would like to get an insight into your daily work :P

How do you read the Person data into the PersonViewModel ?

Is it just a

PersonViewModel pVM = staticHelper.ConvertPersonToPersonViewModel(person);

or is there something cooler?

+1  A: 

Some folks advocate copy constructors.

Others might use reflection to copy properties.

Of course, nothing says you can't use reflection to copy properties while in a copy constructor.

kbrimington
+2  A: 

Simply include the Person object in the view model, don't try to copy the object at all.

public class PersonViewModel 
{
    public Person Person { get; set; }

    ... plus other properties your view model might need
}

then in your controller:

PersonViewModel pVM = new PersonViewModel { Person = person } ;
Clicktricity
so in my ViewModel I have this: public string FirstName { return Person.FirstName;}, Hey thats smart!
Lisa
Thats one way, but you don't need to do that. In your view (page), simply refer to Model.Person.FirstName
Clicktricity
but thats the way Josh smith is doing it! :P The ViewModel delegates the binding to the model... btw. How do you aggregate ViewModel with master-detail relation like CustomerViewModel has a list of OrderViewModel ?
Lisa
Who? In a master-detail your view model would simply include both the master object and list<T> detail. Public class ViewModel { public Master Master { get; set; } public List<Detail> Detail { get; set; } }. Hope that makes sense.
Clicktricity
+2  A: 

Automapper is the best thing since the for loop, maybe even the if statement.

jfar
A: 

I think you're misunderstanding the point of the viewmodel. The viewmodel is supposed to be a mapping / interface to the model, not a copy of it.

IUsedToBeAPygmy
never said its a copy, but the way I see it VERY often implemented makes it look like a copy...
Lisa
It may LOOK like a copy because it's a MAPPING.
IUsedToBeAPygmy