平VS在ASP.NET MVC嵌套视图模型类 [英] Flat vs Nested ViewModel Classes in ASP.NET MVC

查看:110
本文介绍了平VS在ASP.NET MVC嵌套视图模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在两个不同的方法的一些意见,视图模型定义

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

我公司拥有一支一流

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

有关我需要国家的列表来填充CountryID选择一个DropDownList的创建和编辑的意见。我可以看到如何结构详述如下视图模型两大选择。

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.

嵌套视图模型

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

平视图模型

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

在present我偏袒嵌套的方法,因为它节省了我从定义第二次领域,但我希望把它开到更好的方法和意见。

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.

感谢

推荐答案

我个人,因为它导致当您使用部分景色更合理的设计preFER为presentation嵌套的方式。你可能有一个 CompanyPartialView 用于跨知道如何使应用程序全部公司,所以使得很多感揭露公司为嵌套结构。

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.

在另一方面,平面视图模型类是最容易的工作进行数据的输入的。你只要有一大堆表单域,所有映射到各个属性。所以,我的策略通常就是击败他们为数据录入页面和巢他们为presentation /报表页。

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.

这篇关于平VS在ASP.NET MVC嵌套视图模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆