2016-06-23 30 views
-1

我應該創建一個公共靜態類還是使用內部常量? 我工作的一個非常大的應用程序,並注意到在衆多places.This使用常量字符串被用來比較的用戶選擇public static class或const

const string Thatch = "Thatch"; 
const string BrickAndTimberFrame= "Brick And Timber Frame"; 
const string OtherRoof = "Other"; 
etc...... 
etc...... 

我想要做的是在覈心而建立公共靜態類申請(見代碼)。原因是我只需要在一個地方更改/添加一個值。然後

public static class RoofConstruction 
{ 
    public static String Thatch{ get { return "Thatch"; } } 
    public static String BrickAndTimberFrame { get { return "Brick And Timber Frame"; } } 
    etc.... 
    etc.... 
} 

比較功能看起來像這樣

internal bool SlateTileOrConcreteRoof() 
     { 
      return RiskInformation.RoofConstruction.Value == RoofConstruction.Slate || 
        RiskInformation.RoofConstruction == RoofConstruction.TileAndSlate || 
        RiskInformation.RoofConstruction == RoofConstruction.Concrete; 
     } 

請添加任何意見/改進等

+0

更好地在配置文件中使用常量 –

回答

0

一般來說,我認爲「的‘善類™ ’定義特徵,」是「它確實正確的東西,從來沒有(!)‘它,確切地說,」它做到了。 」

當您導出從​​類常量,這表明應用程序的其他部分未知號碼(現在和未來......)將包含測試針對該字符串的邏輯。

因此,只有你能真正回答這個問題:  「他們真的關心‘的值的字符串,’或做自己想做‘的答案,是或否的問題,這是在回答部分基於該字符串的值? ’ 」  這可能會指導你做什麼最好的決定。 (請注意,我不認爲有任何明線規則。 我已經完成了這兩種方法...)