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,则代码返回错误消息,否则返回空字符串。 –