When using MVVM, should you create new viewmodels, or swap out the models?

Posted by ConditionRacer on Programmers See other posts from Programmers or by ConditionRacer
Published on 2013-10-01T15:16:13Z Indexed on 2014/06/07 21:36 UTC
Read the original article Hit count: 129

Filed under:

Say I have a viewmodel like this:

public class EmployeeViewModel
{
    private EmployeeModel _model;

    public Color BackgroundColor { get; set; }
    public Name
    {
        get { return _model.Name; }
        set
        {
            _model.Name = value;
            NotifyPropertyChanged(Name);
        }
    }
}

So this viewmodel binds to a view that displays an employee. The thing to think about is, does this viewmodel represent an employee, or a "displayable" employee. The viewmodel contains some things that are view specific, for instance the background color. There can be many employees, but only one employee view.

With this in mind, when changing the displayed employee, does it make sense to create a new EmployeeViewModel and rebind to the view, or simply swap out the EmployeeModel. Is the distinction even important, or is it a matter of style?

I've always leaned toward creating new viewmodels, but I am working on a project where the viewmodels are created once and the models are swapped out. I'm not sure how I feel about this, though it seems to work fine.

© Programmers or respective owner

Related posts about mvvm