Flat vs Nested ViewModel Classes in ASP.NET MVC

随声附和 提交于 2019-11-27 19:40:29

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!