2016-07-15 44 views
1

GetTranslation函數返回一個字符串。如何簡化這個C#重複代碼?

ExistsHelper函數返回布爾值。

public static class ValidatorNotExistHelper 
{ 
    public static string Country(int id) 
    { 
     return (!ExistsHelper.Country(id)) ? GetTranslation(ConfigTranslationCode.CountryNotExist) : string.Empty; 
    } 

    public static string State(int id) 
    { 
     return (!ExistsHelper.State(id)) ? ConfigTranslationCode.StateNotExist : string.Empty; 
    } 

    public static string City(int id) 
    { 
     return (!ExistsHelper.City(id)) ? ConfigTranslationCode.CityNotExist : string.Empty; 
    } 
} 

正如您可能會注意到代碼正在重複的條件語句。只有ExistsHelper和翻譯消息的功能不同。任何想法,將不勝感激。

編輯:

我也可以有Overloading,對於一個例子:

public static string City(int cityId, int stateId, int countryId) 
{ 
    return (!ExistsHelper.City(cityId, stateId, countryId)) ? ConfigTranslationCode.CityNotExist : string.Empty; 
} 

public static string City(int cityId, int stateId) 
{ 
    return (!ExistsHelper.City(cityId, stateId)) ? ConfigTranslationCode.CityNotExist : string.Empty; 
} 
+0

如果存在'ExistsHelper.City(id)'* *,則返回'string.Empty'。這樣對嗎?我怎樣才能得到*實際*城市的名字? –

+0

ExistsHelper.City(id)返回'boolean'。 「城市名稱」對於此代碼並不重要。如果布爾值爲true,則代碼返回錯誤消息,否則返回空字符串。 –

回答

0

可以簡化泛型一點,但它會讓你您的通話更復雜

創建一個接口爲

public interface IExists 
{ 
    int id{get;} 
} 

,並確保所有可比類實現它 即

public class City:IExists 

然後你可以做

public static class ValidatorNotExistHelper<T> 
     where T:IExists 
{ 
    public static bool Exists(int id,IEnumerable<T> possibles) 
    { 
     return possibles.Any(p=>p.id == id); 
    } 
    public static string ValidateExists(int id,IEnumerable<T> possibles,string ErrorMessage) 
    { 
     return Exists(id,possibles) ? String.Empty : ErrorMessage; 
    } 
    //using lookup Enum 
    public static bool Exists(ClassTypeEnum type, int id) 
    { 
     return Exists(id,Lookup.GetPossibles(type)); 
    } 
    public static string ValidateExists(ClassTypeEnum type,int id) 
    { 
     return Exists(type,id) ? String.Empty : Lookup.GetError(type); 
    } 
} 
//Enum based lookup 
public static class Lookup<T> 
     where T:IExists 
[ 
    public IEnumerable<T> GetPossibles(ClassTypeEnum type) 
    { 
     switch(type) 
     { 
      case ClassTypeEnum.City: 
       return //source of Cities; 
     } 
    } 
    public IEnumerable<T> GetError(ClassTypeEnum type) 
    { 
     switch(type) 
     { 
      case ClassTypeEnum.City: 
       return ConfigTranslationCode.CityNotExist; 
     } 
    } 
? 
+0

當你的答案開始意識到,也許我正在努力重構可能最終失去簡單性的地方? –

+0

它總是一種權衡,一般除非你做的是完全一樣的東西,它很少值得,我的意思是相同的電腦看到它不是人類閱讀它 – MikeT

+0

另一種選擇是添加一個包含城市狀態等的枚舉,然後使用一個查找類,它返回錯誤消息和枚舉的可能值的來源 – MikeT

1

你可以通過在功能,讓您使用的存在性檢查,如:

public static class ValidatorNotExistHelper 
{ 
    public static string Country(int id) 
    { 
     return IdToString(ExistsHelper.Country, 
          GetTranslation(ConfigTranslationCode.CountryNotExist), id); 
    } 

    public static string State(int id) 
    { 
     return IdToString(ExistsHelper.State, 
          ConfigTranslationCode.StateNotExist, id); 
    } 

    public static string City(int id) 
    { 
     return IdToString(ExistsHelper.City, 
          ConfigTranslationCode.CityNotExist, id); 
    } 

    private static string IdToString(Predicate<int> exists, string defaultValue, int id) 
    { 
     return (!exists(id)) ? defaultValue : string.Empty; 
    } 
} 

這會成功地分出一些共享邏輯,但會增加一些複雜性。

如果你只有三種這些方法,我會試着讓它們保持原樣。如果你有很多並且會增加更多,那麼這個重構可能是值得的。

+0

我將擁有20多個函數,但在這些函數中,我也會有重載。也許,我應該修改我原來的問題,並提及超載... –

+0

提供重載示例到原始問題。 –

+0

@CoderAbsolute你可以擁有多個版本的'IdToString'來應付重載,但它開始感到笨重 –