【问题标题】:How to avoid using the same identifier for Class Names and Property Names?如何避免对类名和属性名使用相同的标识符?
【发布时间】:2010-03-30 05:02:54
【问题描述】:

以下是一些类和属性共享相同标识符的示例:

public Coordinates Coordinates { get; set; }
public Country Country { get; set; }
public Article Article { get; set; }
public Color Color { get; set; }
public Address Address { get; set; }
public Category Category { get; set; }

将 POCO 与实体框架一起使用时,此问题会更频繁地发生,因为实体框架使用关系的属性名称。

那该怎么办?使用非标准的类名?

public ClsCoordinates Coordinates { get; set; }
public ClsCountry Country { get; set; }
public ClsArticle Article { get; set; }
public ClsColor Color { get; set; }
public ClsAddress Address { get; set; }
public ClsCategory Category { get; set; }

或者使用更具描述性的属性名称?

public Coordinates GeographicCoordinates { get; set; }
public Country GeographicCountry { get; set; }
public Article WebArticle { get; set; }
public Color BackgroundColor { get; set; }
public Address HomeAddress { get; set; }
public Category ProductCategory { get; set; }

不太理想,但我想可以忍受。

或者只是忍受它?

您的最佳做法是什么?

【问题讨论】:

    标签: c# entity-framework class properties naming-conventions


    【解决方案1】:

    这有时被称为“颜色颜色”问题 - 我的建议就是忍受它。

    C# 语言规范已为此设计,不会成为问题。来自 C# 3 规范的第 7.5.4.1 节:

    在 E.I 形式的成员访问中,如果 E 是单个标识符,如果 E 作为简单名称的含义(第 7.5.2 节) 是一个常量、字段、属性、本地 变量或参数相同 type as 的含义 E as a 类型名称(§3.8),那么两者都可能 E 的含义是允许的。他们俩 E.I 的可能含义是 never 模棱两可,因为我必须是 在这两种情况下都是类型 E 的成员。 换句话说,规则只是 允许访问静态成员 和嵌套类型的 E 其中 a 否则编译时错误 已经发生了。

    (后面有一个例子。)

    显然,当您可以提供更具描述性的属性名称时,这很好 - 但通常最好的名称确实与属性相同。

    这发生在框架本身中 - 例如,HttpWebRequest.CookieContainer 的类型为 CookieContainer,并且有多种类型具有 Evidence 类型的 Evidence 属性。

    【讨论】:

      【解决方案2】:

      我尝试使用更具描述性的属性名称。

      更改类名感觉像是违背了目的,因为大多数开发人员倾向于低估使用良好且描述性的变量/属性名称。

      在你的例子中,例如一个地址可以是

      public Address HomeAddress { get; set; } 
      public Address PostalAddress { get; set; } 
      public Address CompanyAddress { get; set; } 
      

      等等。你可以看到我在做什么。

      【讨论】:

        【解决方案3】:

        如果名称确实适用,我个人并不羞于让类名和属性名匹配。例如,在 Address 类上,有一个名为 Country 的属性被键入为名为 Country 的类是有意义的,并且实际上没有任何名称不是多余的属性。我尝试使用描述性的属性名称来避免冲突,但有时属性名称及其类型是最好使用的名称,使用其他任何名称都会降低而不是提高清晰度。

        我强烈建议不要在类上使用类似匈牙利语的前缀。这简直是​​丑陋的。

        【讨论】:

          【解决方案4】:

          我认为属性的名称应该描述它是什么。如果是 Apple,就直接命名为 Apple。

          为什么有人会为了属性的可读性而在类名中使用匈牙利语。它会降低类名的可读性。

          在访问属性时,您可以使用 This 关键字来防止自己将属性误读为类:

          class Food
          {
              public Apple Apple
              {
                  get;
                  set;
              }
          
              public void DoIt()
              {
                  this.Apple = new Apple();
              }
          }
          
          class Apple
          {
          }
          

          参见 StyleCop 的 SA1101 “验证对本地成员的调用是否以 'this' 为前缀。符号。”

          【讨论】:

            猜你喜欢
            • 2018-02-13
            • 1970-01-01
            • 1970-01-01
            • 2019-12-03
            • 2013-10-18
            • 2019-11-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多