Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flat vs Nested ViewModel Classes in ASP.NET MVC

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

like image 891
Cephas Avatar asked Feb 10 '10 02:02

Cephas


People also ask

What is the difference between ViewModel and model in MVC?

A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.

Is there any use of ViewModel of MVC?

In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization.

What is ViewModel in ASP NET MVC?

ViewModel = Model that is created to serve the view. ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible. ViewModel serves this purpose. View Model is a model class that can hold only those properties that are required for a view.

Can ViewModel have methods?

You can have methods in your ViewModel .

What is ViewModel in ASP NET MVC?

In this article, you will learn the use of ViewModel in ASP.NET MVC. ViewModel is used to encapsulate the multiple entities into single entity. It is basically a combination of data models into single object and rendering by the view.

What is the relationship between a view and a ViewModel?

Ideally, the ViewModel should contain only data and no logic in it. But you can add View Specific logic to ViewModel. Create one ViewModel for each View. i.e there is a one to one relationship between Views and ViewModels. Use ViewModel even for simple scenarios.

What is the model in MVC pattern?

The Model in MVC Pattern stands for View Model and Edit Model. Most people use the term View Model for both View Model and Edit Model. The ViewModel is pretty useful when you have a complex UI, where data needs to be pulled up from several domain models.

What is the use of data model in ASP NET?

It is basically a combination of data models into single object and rendering by the view. Sometimes it is required to show the multiple entities data on view which is coming from different data model classes. So, it includes all the data into single one and makes it flexible to show on View in ASP.NET.


2 Answers

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.

like image 113
Aaronaught Avatar answered Sep 19 '22 21:09

Aaronaught


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.

like image 20
Brian Mains Avatar answered Sep 18 '22 21:09

Brian Mains