Elisabeth
Elisabeth

Reputation: 21206

Reading models into ViewModels and other way round in MVVM - best practice -

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?

Upvotes: 0

Views: 243

Answers (4)

Pygmy
Pygmy

Reputation: 1268

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.

Upvotes: 1

John Farrell
John Farrell

Reputation: 24754

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

Upvotes: 3

Clicktricity
Clicktricity

Reputation: 4209

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 } ;

Upvotes: 3

kbrimington
kbrimington

Reputation: 25642

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.

Upvotes: 1

Related Questions