我的想法是编写一个特定于枚举类型的方法来为我完成该映射。是否有任何标准方法可以实现这个想法?
以下是使用真实应用程序中相同的类名/枚举值等的应用程序代码的简化版本:
// 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)有效/True
第二条路径(NY-AU)无效/False
第三条路径(JP-NY)有效/True [因为 Directionality
是 Between
]
感谢您阅读这个非常长的查询,感谢您提前给出反馈和建议。
类似的帖子: