我正在在线预订网站(航空公司)上工作,我想根据某些设置验证用户/客户选择的路线是否有效。现有代码使用了很多枚举,我发现自己做了很多 if/if else/else 来将特定枚举映射到我想要发生的特定操作。What I want to do is to write a enum-specific method that would do the mapping for me. Is there any standard way to do this?
这是应用程序代码的简化版本,使用来自真实应用程序的相同类名/枚举值等:
// real app has 9 members, shortened for simplicity's sake
public enum RegionType
{
Station,
Country,
All
}
public enum Directionality
{
Between,
From,
To
}
// simplified version
public class Flight
{
public RegionType RegionType { get; set; }
public RegionType TravelRegionType { get; set; }
public string RegionCode { get; set; }
public string TravelRegionCode { get; set; }
public string RegionCountryCode { get; set; }
public string TravelRegionCountryCode { get; set; }
public Directionality Directionality { get; set; }
}
以下是一些示例用法:
// valid flight
Flight flight = new Flight()
{
RegionCode = "NY",
CountryCode = "JP",
RegionType = RegionType.Station,
TravelRegionType = RegionType.Country,
Directionality = Directionality.Between
};
// these are the station code/country code that user selected
// needs to be validated against the Flight object above
var userSelectedRoutes = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("NY", "JP"),
new KeyValuePair<string, string>("NY", "AU"),
new KeyValuePair<string, string>("JP", "NY")
};
我写了一些代码验证来减少嵌套的 if/else if/else 枚举匹配:
private bool IsRouteValid(Directionality direction, string origin,
string destination, string departure, string arrival)
{
// both departure station and arrival station
if (direction == Directionality.Between)
{
return (origin.Equals(departure, StringComparison.OrdinalIgnoreCase)
&& destination.Equals(arrival, StringComparison.OrdinalIgnoreCase)
|| origin.Equals(arrival, StringComparison.OrdinalIgnoreCase)
&& destination.Equals(departure, StringComparison.OrdinalIgnoreCase));
}
else if (direction == Directionality.From)
{
return (origin.Equals(departure,
StringComparison.OrdinalIgnoreCase));
}
else if (direction == Directionality.To)
{
return (destination.Equals(arrival,
StringComparison.OrdinalIgnoreCase));
}
return false;
}
这是我要更改的凌乱代码:
if (flight.RegionType == RegionType.Station
&& flight.TravelRegionType == RegionType.Country)
{
return userSelectedRoutes.Any(route =>
IsRouteValid(flight.Directionality, route.Key, route.Value,
flight.RegionCode, flight.TravelRegionCode));
}
else if (flight.RegionType == RegionType.Country
&& flight.TravelRegionType == RegionType.Station)
{
return userSelectedRoutes.Any(route =>
IsRouteValid(flight.Directionality, route.Key, route.Value,
flight.CountryCode, flight.RegionCode));
}
else if (flight.RegionType == RegionType.Station
&& flight.TravelRegionType == RegionType.Station)
{
return userSelectedRoutes.Any(route =>
IsRouteValid(flight.Directionality, route.Key, route.Value,
flight.RegionCode, flight.TravelRegionCode));
}
else if (flight.RegionType == RegionType.Station
&& flight.TravelRegionType == RegionType.All)
{
return userSelectedRoutes.Any(route =>
IsRouteValid(flight.Directionality, route.Key, route.Value,
flight.RegionCode, route.Value));
}
else if (flight.RegionType == RegionType.All
&& flight.TravelRegionType == RegionType.Station)
{
return userSelectedRoutes.Any(route =>
IsRouteValid(flight.Directionality, route.Key, route.Value,
route.Key, flight.TravelRegionCode));
}
else if (flight.RegionType == RegionType.All
&& flight.TravelRegionType == RegionType.All)
{
return true;
}
else
{
return false;
}
传奇:
RegionCode
= 出发站/出发地
TravelRegionCode
= 到达站/目的地
Between
= 路线必须仅来自给定的出发站和到达站,反之亦然(例如 NY-JP 或 JP-NY)
From
= 从特定车站到任何路线(例如 AU - All)
To
= 到特定车站的任何路线(例如All -AU)
如果您注意到,上述.Any
所有条件都相同,略有变化。如果可能的话,我想减少代码冗余。我使用过KeyValuePair
,所以我在单一数据类型上同时拥有出发站和到达站。
关于如何使此代码不那么混乱/美观的任何想法?我知道我也硬编码IsRouteValid()
,但我 100% 确定Directionality
只能有 3 种可能的组合。RegionType
另一方面,可以有几种组合,例如 Station-Station、Station-Country、Country-Station、Country-Country 等。
预期输出:
第一条路线有效/真 (NY-JP)
第二条路线无效/错误 (NY-AU)
第三条路线有效/真 (JP-NY) [因为Directionality
是Between
]
感谢您阅读这个很长的查询,并提前感谢您的反馈和建议。
类似帖子: