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;
}
如果存在'ExistsHelper.City(id)'* *,則返回'string.Empty'。這樣對嗎?我怎樣才能得到*實際*城市的名字? –
ExistsHelper.City(id)返回'boolean'。 「城市名稱」對於此代碼並不重要。如果布爾值爲true,則代碼返回錯誤消息,否則返回空字符串。 –