我需要确定所有条件是否都符合特定条件。
public class OfferServiceConditionDto
{
public long Id { get; set; }
public Parameter Parameter { get; set; }
public Enums.Condition Condition { get; set; }
public double? Value { get; set; }
[JsonIgnore]
public long? OfferSubServiceId { get; set; }
}
参数有5种情况:
public enum Parameter : int { Length, Width Height, Area, Volume }
病情有6件:
public enum Condition : int
{
LessThan,
LessThanOrEqualTo,
Equals,
GreaterThan,
GreaterThanOrEqualTo,
DoesNotEqual
}
在我的函数中,我得到了元素的宽度,高度和长度,我需要根据OfferServiceConditionDto条件,参数和值进行检查。
到目前为止,我只考虑交换案件,如果那是30张支票。
还有更好的选择吗?
只需提取一些方法即可。这样,您可以轻松地将30(5 * 6)个案例转换为11(5 + 6)个案例。
public static bool CheckCondition(double width, double height, double length, OfferServiceConditionDto dto) {
switch (dto.Parameter) {
case Parameter.Length:
return CheckCondition(dto.Condition, length, dto.Value);
case Parameter.Height:
return CheckCondition(dto.Condition, height, dto.Value);
// plus 3 more...
}
return false;
}
private static bool CheckCondition(Enums.Condition condition, double value1, double? value2) {
if (value2 == null) {
return true; // decide what to do if Value is null
}
switch (condition) {
case Enums.Condition.LessThan:
return value1 < value2.Value;
case Enums.Condition.LessThanOrEqualTo:
return value1 <= value2.Value;
// plus 4 more...
}
return false;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句