简体   繁体   English

财产类型的命名约定

[英]Naming Conventions for Property Types

If you a have Person Property on Address defined like 如果您在地址上定义了Person属性,例如

public class Address
{
   public int AdressId {get; set;}
   public Person AddressesPerson {get;set;}
   public string FullAddress {get; set;}
}

What is the proper conventions, if any, for naming a property of another type? 命名其他类型的属性的正确约定(如果有)是什么?

Name the property according to its semantic meaning. 根据属性的语义来命名属性。 Frankly it's quite odd to have a Person property on an address - it would normally be the other way round. 坦白说,在地址上拥有Person属性是很奇怪的-通常情况恰恰相反。 What's the meaning of the person here? 这里的人是什么意思? What's the association between the address and the person? 地址和人之间有什么关联? You might have Owner or Resident , for example - but in other cases that wouldn't be appropriate. 例如,您可能拥有OwnerResident ,但是在其他情况下不合适。

I don't think there's a hard and fast rule, but generally I like to stick to the following guidelines as much as possible: 我认为没有硬性规定,但总的来说,我希望尽可能遵循以下准则:

  • Don't repeat the name of the class. 不要重复课程的名称。 This one can be surprisingly hard to work around, but it's usually solved by thinking hard about the second rule: 很难解决这一问题,但是通常可以通过认真思考第二条规则来解决:
  • Think about the properties role in the class, and not just what it is. 考虑一下类中的属性角色,而不仅仅是它的含义。 In your example, Owner might be an appropriate property name, since a Person owns a particular Address . 在您的示例中, Owner可能是一个适当的属性名称,因为Person拥有特定的Address

我将从两种类型之间的关系来命名属性,例如居民,所有者等。

Pick one and stick with it. 选择一个并坚持下去。

In your specific case, I wonder why a person is subordinate to an Address, instead of the other way around, but basically just pick the one you want to use and keep using it. 在您的特定情况下,我想知道为什么一个人从属于一个地址,而不是从一个地址到另一个地址,而是基本上只是选择您要使用的地址并继续使用它。 As long as it's not something completely unreadable or against some kind of Team coding guidelines, you're probably good. 只要不是完全不可读或违反某种团队编码准则的东西,您就可能很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM