32

I'm looking for some opinions on two different approaches to ViewModel definition

I have a Company class

public class Company
{
    public string Name { get; set; }
    public int CountryID { get; set; }
}

For the Create and Edit views I need a list of Countries to populate a DropDownList for CountryID selection. I can see two broad choices for how to structure the ViewModel that are detailed below.

Nested ViewModel

public class CompanyCreateEditViewModel
{
    public Company Company { get; set; }
    public IEnumerable<Country> Countries{ get; set; }
....
}

Flat ViewModel

public class CompanyCreateEditViewModel
{
    public string Name { get; set; }
    public int CountryID { get; set; }
    public IEnumerable<Country> Countries{ get; set; }
....
}

At present I'm favoring the Nested approach as it saves me from defining fields for a second time, but I want to throw it open to better approaches and comments.

Thanks

Cephas
  • 794
  • 8
  • 14

2 Answers2

24

I personally prefer the nested approach for presentation because it leads to a more logical design when you use partial views. You might have a CompanyPartialView used all across the application that knows how to render a Company, so it makes a lot of sense to expose the Company as a nested structure.

On the other hand, flat ViewModel classes are the easiest to work with for data entry. You just have a bunch of form fields that all map to individual properties. So my strategy is usually to flatten them for data entry pages and nest them for presentation/report pages.

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
  • 1
    Completely agree. I use nested for presentation - `CompanyViewModel` - and flat for data entry - `CompanyCreateEditViewModel`. – Jarrett Meyer Feb 10 '10 at 02:47
  • @JarrettMeyer Do you use AutoMapper for nested and flat viewmodel? Is there any problem? I'm just curious, I've bad experience when using AutoMapper with nested viewmodel – Willy Oct 02 '14 at 11:44
  • automapper works to me if partial view gets whole master model and fields are generated as m => m.NestedView.Field, otherway you have to map it manually I think – Muflix Nov 07 '16 at 11:58
5

I prefer nested, for several reasons:

  • That's what object oriented is all about.
  • If you use LINQ to SQL or Entities, or an ORM, you can simply pass the ORM objects and not have to pass all kinds of properties.
  • You can pass other views, so you can create separate models for partial views, and if that view uses a partial, you can pass the partial view model class as a property of the view model class.

IMHO, HTH.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • are you giving whole model to the partial view or only the needed one ? I though giving only nested view model is okay, but automapper is not able to recognize that properties from partial view are members of nested model – Muflix Nov 07 '16 at 12:01
  • 1
    It depends. What does the partial view need? Is the partial view part of a larger form (which then you'd need the whole model so that the ID pathing works correctly). If the partial view is self contained, keeping it isolated to the submodel is OK. In your instance, if you need AUtoMapper to recognize certain properties, sounds like a reason to pass the whole model, possibly. – Brian Mains Nov 07 '16 at 14:47