你如何命名你的ViewModel类?

时间:2022-09-25 10:23:35

What kind of naming convention is appropriate for ViewModel classes?

什么样的命名约定适合ViewModel类?

Example: for HomeController, Index view? HomeIndexViewModel doesn't seem right.

示例:对于HomeController,索引视图? HomeIndexViewModel似乎不对。

6 个解决方案

#1


2  

EmployeesViewData.

EmployeesViewData。

That's what I use and what I've seen in sample applications as well.

这就是我使用的以及我在示例应用程序中看到的内容。

About your added example: Imho the name of the class should specify what kind of data it contains. "....IndexViewData" is rather meaningless. What exactly is displayed on that page? Try to summarize it in 1 or 2 word(s) and add 'ViewData' behind it.

关于您添加的示例:Imho类的名称应指定它包含的数据类型。 “...... IndexViewData”毫无意义。该页面上显示的内容究竟是什么?尝试用1或2个单词对其进行总结,并在其后面添加“ViewData”。

Or else just take the name of the controller and drop the "index". HomeViewData sounds fine to me.

或者只是取控制器的名称并删除“索引”。 HomeViewData听起来不错。

#2


22  

I use the following pattern because it's clear and unambiguous :

我使用以下模式,因为它清晰明确:

  • Model : Foo
  • 型号:Foo
  • View : FooView
  • 查看:FooView
  • ViewModel : FooViewModel
  • ViewModel:FooViewModel

#3


1  

I try to keep my presentation model names agnostic of the kind of presentation they will be presented with. I may use my model object for an ASP.NET view initially, but later on I may also use it in a WCF or WinForms app. I try to name my models such that they logically describe what they contain, without muddying them up with "ViewData", "ViewModel", "Model", etc.

我试着让我的演示模型名称与他们将要呈现的演示类型无关。我最初可能会将我的模型对象用于ASP.NET视图,但稍后我也可以在WCF或WinForms应用程序中使用它。我尝试命名我的模型,使它们在逻辑上描述它们包含的内容,而不是用“ViewData”,“ViewModel”,“Model”等混淆它们。

Examples:

例子:

ProductsWithPageInfo
ProductWithAttributesAndTags
ClientAndBillingDetail
UserAccountWithAssociatedGroups

Etc.

等等。

#4


0  

I prefer HomeViewModel, or using the previous employee example, CreateEmployeeViewModel, EditEmployeeViewModel, etc. The idea is that the "ViewModel" emphasises the fact that we're dealing with presentation concerns, and disambiguates the ViewModels from any domain model objects that you may have.

我更喜欢HomeViewModel,或者使用之前的员工示例,CreateEmployeeViewModel,EditEmployeeViewModel等。我们的想法是“ViewModel”强调我们正在处理表示问题,并从您可能拥有的任何域模型对象中消除ViewModels的歧义。 。

#5


0  

I've been using the term Envelope as part of my names, which I started using shortly before I read chapter 1 of the Wrox book and discovered the more commonly-accepted term is ViewModel.

我一直在使用术语Envelope作为我名字的一部分,我在阅读Wrox书的第1章之前不久就开始使用它,并发现更常被接受的术语是ViewModel。

However, these ViewEnvelopes that I create are only for very simple encapsulation when I have two or more (typically unrelated) strongly-typed models I'd like to hand to my View. They do not contain any other functionality---the envelope is intended only as delivery mechanism in this sense, whereas the term ViewModel, for me, seems less descriptive for how I'm using it, and also possibly more ambiguous as to what its real purpose is.

但是,我创建的这些ViewEnvelopes只用于非常简单的封装,当我有两个或更多(通常是不相关的)强类型模型时,我想交给我的View。它们不包含任何其他功能---信封只是作为这种意义上的传递机制,而对我来说,ViewModel这个术语对我如何使用它似乎不太具有描述性,而且对于它的含义也可能更加含糊不清真正的目的是。

I might create, for example, a CustomerUpdateEnvelope class which exists solely to deliver a Customer object and an unrelated NewsTicker object, for example, to my Customer Update view.

例如,我可能会创建一个CustomerUpdateEnvelope类,该类仅用于传递Customer对象和不相关的NewsTicker对象,例如,我的Customer Update视图。

#6


0  

Rather late for this particular thread, but I've written a few slightly more detailed suggestions about ViewModel naming conventions in a blog post, which may be useful to others.

对于这个特定的线程来说相当晚,但我在博客文章中写了一些关于ViewModel命名约定的更详细的建议,这可能对其他人有用。

#1


2  

EmployeesViewData.

EmployeesViewData。

That's what I use and what I've seen in sample applications as well.

这就是我使用的以及我在示例应用程序中看到的内容。

About your added example: Imho the name of the class should specify what kind of data it contains. "....IndexViewData" is rather meaningless. What exactly is displayed on that page? Try to summarize it in 1 or 2 word(s) and add 'ViewData' behind it.

关于您添加的示例:Imho类的名称应指定它包含的数据类型。 “...... IndexViewData”毫无意义。该页面上显示的内容究竟是什么?尝试用1或2个单词对其进行总结,并在其后面添加“ViewData”。

Or else just take the name of the controller and drop the "index". HomeViewData sounds fine to me.

或者只是取控制器的名称并删除“索引”。 HomeViewData听起来不错。

#2


22  

I use the following pattern because it's clear and unambiguous :

我使用以下模式,因为它清晰明确:

  • Model : Foo
  • 型号:Foo
  • View : FooView
  • 查看:FooView
  • ViewModel : FooViewModel
  • ViewModel:FooViewModel

#3


1  

I try to keep my presentation model names agnostic of the kind of presentation they will be presented with. I may use my model object for an ASP.NET view initially, but later on I may also use it in a WCF or WinForms app. I try to name my models such that they logically describe what they contain, without muddying them up with "ViewData", "ViewModel", "Model", etc.

我试着让我的演示模型名称与他们将要呈现的演示类型无关。我最初可能会将我的模型对象用于ASP.NET视图,但稍后我也可以在WCF或WinForms应用程序中使用它。我尝试命名我的模型,使它们在逻辑上描述它们包含的内容,而不是用“ViewData”,“ViewModel”,“Model”等混淆它们。

Examples:

例子:

ProductsWithPageInfo
ProductWithAttributesAndTags
ClientAndBillingDetail
UserAccountWithAssociatedGroups

Etc.

等等。

#4


0  

I prefer HomeViewModel, or using the previous employee example, CreateEmployeeViewModel, EditEmployeeViewModel, etc. The idea is that the "ViewModel" emphasises the fact that we're dealing with presentation concerns, and disambiguates the ViewModels from any domain model objects that you may have.

我更喜欢HomeViewModel,或者使用之前的员工示例,CreateEmployeeViewModel,EditEmployeeViewModel等。我们的想法是“ViewModel”强调我们正在处理表示问题,并从您可能拥有的任何域模型对象中消除ViewModels的歧义。 。

#5


0  

I've been using the term Envelope as part of my names, which I started using shortly before I read chapter 1 of the Wrox book and discovered the more commonly-accepted term is ViewModel.

我一直在使用术语Envelope作为我名字的一部分,我在阅读Wrox书的第1章之前不久就开始使用它,并发现更常被接受的术语是ViewModel。

However, these ViewEnvelopes that I create are only for very simple encapsulation when I have two or more (typically unrelated) strongly-typed models I'd like to hand to my View. They do not contain any other functionality---the envelope is intended only as delivery mechanism in this sense, whereas the term ViewModel, for me, seems less descriptive for how I'm using it, and also possibly more ambiguous as to what its real purpose is.

但是,我创建的这些ViewEnvelopes只用于非常简单的封装,当我有两个或更多(通常是不相关的)强类型模型时,我想交给我的View。它们不包含任何其他功能---信封只是作为这种意义上的传递机制,而对我来说,ViewModel这个术语对我如何使用它似乎不太具有描述性,而且对于它的含义也可能更加含糊不清真正的目的是。

I might create, for example, a CustomerUpdateEnvelope class which exists solely to deliver a Customer object and an unrelated NewsTicker object, for example, to my Customer Update view.

例如,我可能会创建一个CustomerUpdateEnvelope类,该类仅用于传递Customer对象和不相关的NewsTicker对象,例如,我的Customer Update视图。

#6


0  

Rather late for this particular thread, but I've written a few slightly more detailed suggestions about ViewModel naming conventions in a blog post, which may be useful to others.

对于这个特定的线程来说相当晚,但我在博客文章中写了一些关于ViewModel命名约定的更详细的建议,这可能对其他人有用。