ASP.NET 控件的命名准则是什么?
我们正在制定我们希望在开发团队中使用的设计指南,并且今天开始讨论如何命名 ASP.NET 控件。 我说的是我们的好朋友标签、文本框、按钮等。
我们提出了以下三种可能性并进行了投票:(示例是用于输入/显示名字的文本框)
- 将控件类型作为后缀添加到控件 ID : [FirstName
_
TextBox] 或 [FirstName_
tbx] - 将控件类型添加为控件 ID 的前缀 [tbxFirstName]
- 将控件的 ID 设置为 FirstName 和名称相关字段(如文本框或验证器的标签)如选项 2 [lblTextBox] 中。
我们最终决定使用选项 2。它不像选项 1 那样冗长,而且我喜欢它在控件名称之前指定它是什么控件。
我的问题是 Microsoft 是否发布了有关这些前缀的任何指南,或者您对我们的决定是否有任何意见。
We are in the process of nutting out the design guidelines we would like to use in our development team and got into a discussion today around how ASP.NET controls should be named. I am talking about our good friends Label, TextBox, Button etc.
We came up with the following three possibilities that we voted on: (Example is a TextBox to enter/display a FirstName)
- Add the control type as a postfix to your controls ID: [FirstName
_
TextBox] or [FirstName_
tbx] - Add the control type as a prefix to your controls ID [tbxFirstName]
- Set the ID of the control to FirstName and name related fields (like a label for the textbox or a validator) as in option 2 [lblTextBox].
We ended up deciding to use option 2. It's not as verbose as option 1 and I like that it specifies what control it is before the name of the control.
My question is whether Microsoft has released any guidelines for these prefixes and or if you have any comments about our decision.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
当您将“TextBox1”添加到页面时,Visual Studio 添加它的原因是因为 Microsoft 无法知道您打算如何使用它。 将其命名为“Control1”会太混乱,因为它可以是任意数量的控件。
Microsoft 提供了有关 OO 命名约定的一般指南,但没有专门针对命名 UI 控件。 由于 UI 控件最终是代码中使用的变量,因此它们应该遵循与任何其他变量相同的约定 - 没有匈牙利符号前缀。
主要原因是...
示例
The reason Visual Studio adds "TextBox1" when you add it to the page is because Microsoft has no way of knowing how you intend to use it. Naming it "Control1" would be too confusing because it could be any number of controls.
Microsoft provides guidance in general for OO naming conventions, but not specifically for naming UI controls. Since UI controls are ultimately variables used in code, they should follow the same convention as any other variable - no hungarian notation prefix.
The main reasons are...
Examples
不确定微软的官方标准,但这就是我在我的开发生涯中所做的。
我通常在控件功能的名称前面缩写控件类型。
我保留了缩写小写和控件名称 CamelCase。
例如,用户名的文本框变为 tbUserName
这是我使用的标准缩写的列表:
Not sure about Microsoft official standards, but this is what i've done through out my development career.
I generally abbreviate the control type in front of the the name of what the control does.
I keep the abbreviation lower case and the control's name CamelCase.
E.g. A texbox for username becomes tbUserName
Here is a list of standard abbreviations I use:
我发现大多数时候我关心的是控件的用途,而不是当前使用什么控件类型来捕获该数据,因此我更喜欢在控件类型之前选择信息类型,因此我可以在IDE 中的排序列表:
VS:
I find that most of the time I care about what kind of information the control is for rather than what control type is currently being used to capture that data, so I prefer the type of information before the control type, so I can find it in a sorted list in the IDE:
VS:
我不确定有关 ASP.NET 的指南,但在 Microsoft 的《框架设计指南》一书中,有几个有关类成员命名的最佳实践指南。 由于 ASP.NET 控件在大多数情况下会产生适当类型的受保护字段,因此我认为这些命名准则也适用于 ASP.NET 控件。 事实上,代码分析并不区分控制引用字段和其他字段。
这些指南建议使用暗示逻辑用途的命名方案,而不是类型描述性变体。 有几个原因。 对于开发人员来说,前缀意味着一种类型,由于以后的更改,该类型可能会不正确。 它在代码维护方面增加了一个额外的步骤。 如果将 Button 控件更改为 LinkButton 控件,则还需要更改名称以更正前缀。
因此,我会调用该控件 FirstNameEdit 等......
I'm not sure of the guidelines regarding ASP.NET, but in the book Framework Design Guidelines from Microsoft, there are several best-practice guidelines about naming of class members. Since ASP.NET Controls in most cases result in a protected field of the appropriate type, I consider these naming guidelines to apply for ASP.NET controls as well. In fact Code Analysis does not differentiate on Control reference fields and other fields.
These guidelines recommend using a naming scheme that implies the logical use rather than a type-descriptive variant. There are several reasons for this. The prefix is implies a type to the developer that might not be correct due to later changes. It adds an extra step in code maintainence. If you change your Button control into a LinkButton control the name also needs to be changed to correct the prefix.
For that reason I would call the control FirstNameEdit etc...
Microsoft 确实在这里提供了一些指导。
当您将控件拖到 Web 表单上时,您会自动获得类似“TextBox1”的内容。 IDE 告诉您应该根据您的特定需求更改“1”部分。
在这种情况下,“TextBoxFirstName”似乎是正确的选择。
Microsoft does provide some guidance here.
When you drag a control onto a web form you get something like "TextBox1" automatically. That's the IDE telling you that you should change the "1" part for your specific needs.
In that case, "TextBoxFirstName" seems like the way to go.
我更喜欢选项 1 的两个原因:
话虽如此,我正在考虑更改为 FirstNameCtrl,因为 csgero 指出了更改控件类型的原因。 那么为什么要费心使用任何后缀或前缀来减少/消除与 asp/win 表单属性冲突的可能性。
Two reasons why I prefer option 1:
Having said that I am considering changing to FirstNameCtrl for the reason csgero pointed out about changing control types. So why bother with any postfix or prefix, to reduce/remove the posibility of conflicts with asp/win form properties.
我认为使用选项 1 更好,因为可以很容易地通过其含义及其用法找到该字段,以便以后理解编程编码。 此外,使用 IntelliSense 可以更方便地找到我们在编程代码中使用该字段的位置。 因此我可以通过有意义的字段的名称找到正确的控件。 我不记得我对该字段使用哪种控件,但我可以通过使用有意义的字段名称而不是控件类型来找到该字段,例如我想查找“城市”控件,我只需输入“城市”,智能将向我显示该控件的所有信息,但如果我不记得我使用的是哪种控件,我不知道该开始......
I think it is better to use option 1 because it is easy to find the field by its meaning and its usage to understand programming coding down the road . Also,it is more usable with IntelliSense to find where we use this field for in our programming code. Therefore I can find the right control by the name of the meaningful field. I will not remmember what kind of control I use for this field but I can find this field by using the meaningful of field name instead of the type of control example I want to find "City" control , I just typ "City" , Intellisence will show me all information for this control but if I do not remember what kind of control I use for , I do not know what to begin....
标准控件:
btn Button
cb CheckBox
cbl CheckBoxList
ddl DropDownList
fu FileUpload
hdn HiddenField
lnk Hyperlink
img Image
ibtn(btn) ImageButton
lbl Label
lbtn(btn) LinkBut
ton lb ListBox
lit Literal
mv MultiView
pnl Panel
ph PlaceHolder
rb RadioButton
rbl RadioButtonList
tbl Table
txt TextBox
v查看
数据控件
dtl DataList
dp DataPager
dtv DetailsView
ets EntityDataSource
fv FormView
gv GridView
lds LinqDataSource
lv - ListView
ods ObjectDataSource
qe QueryExtender
rpt Repeater
smd SiteMapDataSource
sds SqlDataSource
xds XmlDataSource
验证
控件 cpv CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
requiredFieldValidator
与 ValidationSummary
验证控制:
cpv // CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
rfvRequiredFieldValidator
STANDARD CONTROLS:
btn Button
cb CheckBox
cbl CheckBoxList
ddl DropDownList
fu FileUpload
hdn HiddenField
lnk Hyperlink
img Image
ibtn(btn) ImageButton
lbl Label
lbtn(btn) LinkButton
lb ListBox
lit Literal
mv MultiView
pnl Panel
ph PlaceHolder
rb RadioButton
rbl RadioButtonList
tbl Table
txt TextBox
v View
DATA CONTROLS
dtl DataList
dp DataPager
dtv DetailsView
ets EntityDataSource
fv FormView
gv GridView
lds LinqDataSource
lv - ListView
ods ObjectDataSource
qe QueryExtender
rpt Repeater
smd SiteMapDataSource
sds SqlDataSource
xds XmlDataSource
VALIDATION CONTROLS
cpv CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
rfv RequiredFieldValidator
vs ValidationSummary
VALIDATION CONTROLS:
cpv // CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
rfv RequiredFieldValidator
我们也使用第 2 种方法,但我并不完全相信这是一个好方法。 它是“坏”类型的匈牙利表示法,这意味着前缀表示类型(语法)而不是目的(语义)。 这样做的问题是,最初作为 TextBox 的内容可能后来变成 DropDown,然后变成 RadioButtonGroup,并且每次都必须重命名该控件。
We also use number 2, however I'm not totally convinced it is a good approach. It is Hungarian notation from the "bad" kind, meaning that the prefixes signal the type (syntax) and not the purpose (semantics). The problem with this is that what starts as a TextBox might later become a DropDown and then a RadioButtonGroup, and you'll have to rename the control each time.
几乎每个人都使用匈牙利语风格的前缀(选项 2)。
语义命名不太有用,因为“Firstname”实际上是 texbox.Text 值,而不是文本框本身。
Almost everyone uses Hungarian-style prefixes (option 2).
Semantic naming is less useful because "Firstname" is actually the texbox.Text value, not the textbox itself.
我倾向于使用控件类型作为前缀,然后使用控件的名称,但我总是使用 CamelCase,因此在您的示例中,对于您可能拥有的不同类型的控件。
出于智能感知的原因,我也总是完全限定名称控件,所以我不会执行以下任何操作...
但最终取决于个人喜好
I tend to go with the control type as a prefix and the name of the control afterwards but I always CamelCase so in your example for different types of controls you might have..
For intellisense reasons I also always fully qualify the name of the control so I wouldn't do any of the following...
But ultimately down to personal preference
我也一直在为这个问题苦苦挣扎。 我曾经使用“匈牙利风格前缀”。
现在我采取不同的方法,我尝试将控件视为类中的私有字段。 我不会用它们的类型预先修复我的私有字段,那么我为什么要对文本框执行此操作呢?
所以以前是:
变成:
说实话,我不在乎“名称”控件是文本框还是其他什么,我只想要它的值。
如果您谈论您的控件或另一个字段/变量还不够清楚,我认为您应该重新考虑该字段/变量的名称或您的类的函数(这意味着它可能有点大?) 。
I've been struggling with this problem too. I used to use the "hungarian style prefix".
Now I take a different approach, I try to see the controls as private fields from my class. I don't pre- of postfix my private fields with their type, so why should I do that to an TextBox?
So what used to be:
Becomes:
To be honest, I couldn't care less if the "name" control is a text box or what else, I just want it's value.
And if it's not clear enough if your talking about your control or about another field/variable, I think you should either reconsider the name of that field/variable or the function of you class (meaning it might be a little bit to big?).
如果您从代码维护的角度来看,在您两年前编写代码后,最好的表示法是什么。 尽管我们尽力确保表单上没有太多字段,但我们都知道这种情况有时会发生。 如果我们通过预先考虑控件类型来使用匈牙利类型表示法,我认为会更容易看到该值来自哪里,而不必在变量名称不明显的情况下找出它。 如果您使用任何类型的重构工具,那么更改控件的名称将自动更改代码,从而减少更改控制争论。
If you look at it from a code maintainance point of view what would be the best notation to look at after you did the code 2 years ago. Although we try to ensure that forms don't have too many fields on them we all know that this sometimes happens. If we used the Hungarian type notation by prepending the control type I think it would be easier to see where that value is coming from instead of having to figure it out in the event the variable name doesn't make it obvious. If you are using any type of Refactoring tool then changing the name of the control would change the code automatically thereby reducing the change control arguement.
我认为这里没有正确或错误的答案,无论您决定如何,我认为最重要的方面是在实际编码时保持一致。
I don’t think there’s a right or wrong answer here, whatever you decide, I think the most important aspect is just to be consistent when it actually comes to coding.
我使用 uxCity,这样您就知道它绝对是用户界面控件,而不是其他对象,但如果您从 TextBox 转到 DropDownList,则不需要更改它。
但是,如果我有一个 DropdownList 和 一个文本框,我需要使用 dlCity 和 txtCity
或者我会使用组合 cboCity。
当您的名称仅限于 8 个字符并且没有智能感知或调试突出显示时,匈牙利表示法就很常见。 这是一门学科,你可以看到,如果编码风格正确,代码很可能是正确的。 它也用于变量,因此您可以阅读代码并理解它,因为它是 DIY 类型强制。
但是,我确实使用 CityTextbox、CityTextboxLabel
CityUx、CityUxLbl
这完全取决于谁为项目制定标准。
I use uxCity so that you know it's definitely the User Interface control and not some other object but you don't need to change it if you go from a TextBox to a DropDownList.
However If I had a DropdownList and a Textbox, I'd need to use dlCity and txtCity
Or I'd use a combo cboCity.
Hungarian notation was de rigeur when you were limited to 8-character names and no intellisense or debug highlighting. It was a discipline and you could see that if the coding style was correct, the code was likely to be correct. It was used on variables too so you could read the code and understand it as it was a DIY type enforcement.
However, I do use CityTextbox, CityTextboxLabel
CityUx, CityUxLbl
It all depends on who is setting the standards on the project.
我发现使用匈牙利表示法的唯一原因是 IDE 没有智能感知,很难弄清楚什么是什么,所以 iCounter 是一个整数。
但是使用 Brief 进行开发的时代早已一去不复返了,IDE 会向您显示info in a second
然后你继承了VB.NET代码并且它不区分大小写,那么你该怎么办?
lbl名字,txt名字
一个是标签,另一个是文本框
那么如何在不区分大小写的情况下命名它们并真正知道它们是什么?
ux名字 & uxFirstName 不起作用,
我发现的唯一答案是使用匈牙利表示法,是的,我吐在嘴里。 然后又是 vb.net,它应该区分大小写,因为 .net 是并且所有都编译为 IL。
I found the only reason to use Hungarian notation was that the IDE did not have intellisense and it was not easy to figure out what was what so iCounter was an integer
However the time of using brief to develop is long gone and the IDE shows you the info in a second
Then you inherit VB.NET code and it is not case sensitive so what do you do?
lblFirstName, txtFirstName
One is a label the other is the textbox
So how do you name these without case sensitivity and actually know what they are?
uxFirstName & uxFirstName does not work
the only answer i have found is to use Hungarian notation, yes i threw up in my mouth. Then again its vb.net and it should be case sensitive since .net is and all of the compiles to IL.
不太确定有什么指导方针,我怀疑有,但我也总是使用 2 号!
Not really sure about any guidelines, i suspect there are, but I always use number 2 as well!
这些实际上只是基于您的偏好,但是是的,所描述的选项 2 不太详细,甚至在显示其名称之前就向您指示了控件的类型。
these are really just based on your preferences but yes, option 2 as described is less verbose and indicates to you the type of control even before its name is shown.