asp.net MVC 3中视图模型的范围

时间:2022-10-05 11:27:53

I have read online that it is bad practice to use a "kitchen sink" model:

我在网上看到使用“厨房水槽”模型是不好的做法:

Rule #3 – The View dictates the design of the ViewModel. Only what is required to render a View is passed in with the ViewModel.

规则#3 - View规定了ViewModel的设计。只有使用ViewModel传递渲染视图所需的内容。

If a Customer object has fifty properties, but one component only shows their name, then we create a custom ViewModel type with only those two properties.

如果Customer对象有50个属性,但只有一个组件显示其名称,那么我们创建一个只包含这两个属性的自定义ViewModel类型。

Jimmy Bogard's subsequent explanation of how this is good, however, left me a little questioning. It'd be so easy to have my Model just contain a list of Customers, I could even use my POCO's.

然而,吉米·博加德随后解释这是好的,给我一点质疑。让我的模型只包含一个客户列表是非常容易的,我甚至可以使用我的POCO。

So now I get to create custom little view model fragments for every page on the site? Every page that uses a Customer property would get one, but of course could not be shared since some of the information is extraneous, if one page used Age but not Name, for example. Two new mini view model classes right?

那么现在我可以为网站上的每个页面创建自定义的小视图模型片段?使用Customer属性的每个页面都会获得一个,但当然无法共享,因为某些信息是无关紧要的,例如,如果一个页面使用的是Age而不是Name。两个新的迷你视图模型类吧?

This is very time consuming, and seems like it'll lead to a million little custom view models - can someone elaborate as to the utility of this approach and why the easier approach is bad?

这是非常耗时的,似乎它会导致一百万个小型自定义视图模型 - 有人可以详细说明这种方法的实用性以及为什么更简单的方法是坏的?

3 个解决方案

#1


3  

View model class can be used not only to transfer values, but it also defines data types (data annotations), validation rules and relations different then ones used in model. Some advantages that come to my mind right now:

View模型类不仅可以用于传递值,还可以定义数据类型(数据注释),验证规则和关系,而不是模型中使用的数据类型。我现在想到的一些优点:

  • There are different validation rules when you change user's password, change his basic data or his subscription setting. It can be complicated to define all these rules in one model class. It looks much better and cleaner when different view models are used.
  • 更改用户密码,更改基本数据或订阅设置时,有不同的验证规则。在一个模型类中定义所有这些规则可能很复杂。当使用不同的视图模型时,它看起来更好更清洁。

  • Using view model can also give you performance advantages. If you want to display user list, you can define view model with id and name only and use index to retrieve it from database. If you retrieved whole objects and pass it to view, you transfer more data from database than you need to.
  • 使用视图模型还可以为您提供性能优势。如果要显示用户列表,可以仅定义具有id和name的视图模型,并使用index从数据库中检索它。如果检索到整个对象并将其传递给视图,则会从数据库传输的数据超出您的需要。

  • You can define display, and editor templates for view models and reuse them on different pages using html helpers. It looks much worse, when you define templates for model POCOs.
  • 您可以为视图模型定义显示和编辑器模板,并使用html帮助程序在不同页面上重复使用它们。当您为模型POCO定义模板时,情况看起来更糟。

#2


2  

If you would use your POCO objects as view models, you would essentially be showing your private objects and break the encapsulation. This in turn would make your model hard to change without altering the corresponding views.

如果您将POCO对象用作视图模型,那么您实际上将显示您的私有对象并打破封装。这反过来会使您的模型难以在不改变相应视图的情况下进行更改。

Your data objects may contain details that are appropriate only to the data access layer. If you expose those things to the view, someone might alter those values that you did not expect to be altered and cause bugs.

您的数据对象可能包含仅适用于数据访问层的详细信息。如果将这些内容暴露给视图,有人可能会更改那些您不希望被更改的值并导致错误。

Many of the same reasons as for having private members in OO languages apply to this reasoning. That being said, it's still very often broken because it's a lot of extra work to create all these "throw-away" models that only gets used once. There exists frameworks for creating these sorts of models, though the name eludes me, that can tie objects together and pick out the interesting properties only which takes away some of the drudgery from creating specific view models.

许多与OO语言中的私人成员相同的原因适用于这种推理。话虽如此,它仍然经常被打破,因为创建所有这些只能使用一次的“丢失”模型需要做很多额外的工作。存在用于创建这些类型的模型的框架,尽管名称包括我,它可以将对象绑定在一起并且仅选择有趣的属性,这会消除一些创造特定视图模型的苦差事。

#3


0  

Your View Model tells the View how data should be shown. It expresses the model. I don't think its necessary to have two view models unless you have two ways to express your model. Just because you have two pages, doesn't mean you will be showing the data any different way, so I wouldn't waste time making two mini View Models when it can be in one reusable view model, Imagine if later you have a page that needs Name and Age, you would create another view model? It's absolutely silly. However, if you had two pages both showing 'Age' and it needed to be shown in a different way, then I would create another one.

您的视图模型告诉View如何显示数据。它表达了模型。除非你有两种表达模型的方法,否则我认为没有必要拥有两个视图模型。仅仅因为你有两个页面,并不意味着你将以任何不同的方式显示数据,所以当它可以在一个可重复使用的视图模型中时,我不会浪费时间制作两个迷你View模型,想象一下,如果以后你有一个页面需要Name和Age,你会创建另一个视图模型吗?这绝对是愚蠢的。但是,如果你有两个页面都显示'Age'而且需要以不同的方式显示,那么我会创建另一个。

#1


3  

View model class can be used not only to transfer values, but it also defines data types (data annotations), validation rules and relations different then ones used in model. Some advantages that come to my mind right now:

View模型类不仅可以用于传递值,还可以定义数据类型(数据注释),验证规则和关系,而不是模型中使用的数据类型。我现在想到的一些优点:

  • There are different validation rules when you change user's password, change his basic data or his subscription setting. It can be complicated to define all these rules in one model class. It looks much better and cleaner when different view models are used.
  • 更改用户密码,更改基本数据或订阅设置时,有不同的验证规则。在一个模型类中定义所有这些规则可能很复杂。当使用不同的视图模型时,它看起来更好更清洁。

  • Using view model can also give you performance advantages. If you want to display user list, you can define view model with id and name only and use index to retrieve it from database. If you retrieved whole objects and pass it to view, you transfer more data from database than you need to.
  • 使用视图模型还可以为您提供性能优势。如果要显示用户列表,可以仅定义具有id和name的视图模型,并使用index从数据库中检索它。如果检索到整个对象并将其传递给视图,则会从数据库传输的数据超出您的需要。

  • You can define display, and editor templates for view models and reuse them on different pages using html helpers. It looks much worse, when you define templates for model POCOs.
  • 您可以为视图模型定义显示和编辑器模板,并使用html帮助程序在不同页面上重复使用它们。当您为模型POCO定义模板时,情况看起来更糟。

#2


2  

If you would use your POCO objects as view models, you would essentially be showing your private objects and break the encapsulation. This in turn would make your model hard to change without altering the corresponding views.

如果您将POCO对象用作视图模型,那么您实际上将显示您的私有对象并打破封装。这反过来会使您的模型难以在不改变相应视图的情况下进行更改。

Your data objects may contain details that are appropriate only to the data access layer. If you expose those things to the view, someone might alter those values that you did not expect to be altered and cause bugs.

您的数据对象可能包含仅适用于数据访问层的详细信息。如果将这些内容暴露给视图,有人可能会更改那些您不希望被更改的值并导致错误。

Many of the same reasons as for having private members in OO languages apply to this reasoning. That being said, it's still very often broken because it's a lot of extra work to create all these "throw-away" models that only gets used once. There exists frameworks for creating these sorts of models, though the name eludes me, that can tie objects together and pick out the interesting properties only which takes away some of the drudgery from creating specific view models.

许多与OO语言中的私人成员相同的原因适用于这种推理。话虽如此,它仍然经常被打破,因为创建所有这些只能使用一次的“丢失”模型需要做很多额外的工作。存在用于创建这些类型的模型的框架,尽管名称包括我,它可以将对象绑定在一起并且仅选择有趣的属性,这会消除一些创造特定视图模型的苦差事。

#3


0  

Your View Model tells the View how data should be shown. It expresses the model. I don't think its necessary to have two view models unless you have two ways to express your model. Just because you have two pages, doesn't mean you will be showing the data any different way, so I wouldn't waste time making two mini View Models when it can be in one reusable view model, Imagine if later you have a page that needs Name and Age, you would create another view model? It's absolutely silly. However, if you had two pages both showing 'Age' and it needed to be shown in a different way, then I would create another one.

您的视图模型告诉View如何显示数据。它表达了模型。除非你有两种表达模型的方法,否则我认为没有必要拥有两个视图模型。仅仅因为你有两个页面,并不意味着你将以任何不同的方式显示数据,所以当它可以在一个可重复使用的视图模型中时,我不会浪费时间制作两个迷你View模型,想象一下,如果以后你有一个页面需要Name和Age,你会创建另一个视图模型吗?这绝对是愚蠢的。但是,如果你有两个页面都显示'Age'而且需要以不同的方式显示,那么我会创建另一个。