What is the appropriate granularity in building a ViewModel?

Posted by JasCav on Stack Overflow See other posts from Stack Overflow or by JasCav
Published on 2012-10-14T01:22:49Z Indexed on 2012/10/14 3:37 UTC
Read the original article Hit count: 209

Filed under:
|

I am working on a new project, and, after seeing some of the difficulties of previous projects that didn't provide enough separation of view from their models (specifically using MVC - the models and views began to bleed into each other a bit), I wanted to use MVVM.

I understand the basic concept, and I'm excited to start using it. However, one thing that escapes me a bit - what data should be contained in the ViewModel?

For example, if I am creating a ViewModel that will encompass two pieces of data so they can be edited in a form, do I capture it like this:

public PersonAddressViewModel {
    public Person Person { get; set; }
    public Address Address { get; set; }
}

or like this:

public PersonAddressViewModel {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string StreetName { get; set; }
    // ...etc
}

To me, the first feels more correct for what we're attempting to do. If we were doing more fine grain forms (maybe all we were capturing was FirstName, LastName, and StreetAddress) then it might make more sense to go down to that level. But, I feel like the first is correct since we're capturing ALL Person data in the form and ALL Address data. It seems like it doesn't make sense (and a lot of extra work) to split things apart like that.

Appreciate any insight.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about viewmodel