[英]C# best practice with using multiple similar methods with different properties?
I'm trying to simplify and reduce my code.我正在尝试简化和减少我的代码。
I have a method like this:我有这样的方法:
private bool FindMatchingValue(string value, string response, string dataType) {
bool isFound = false;
switch (dataType) {
case "Date":
var parsedDate = DateTimeOffset.Parse(response).ToString("yyyy-MM-dd");
isFound = checkEquality(value, parsedDate);
break;
case "Time":
var parsedTime = DateTimeOffset.Parse(response).ToString("HH:mm:00.000");
isFound = checkEquality(value, parsedTime);
break;
}
return isFound;
}
I have another method which takes a list of strings like this:我有另一种方法,它采用这样的字符串列表:
private bool FindMatchingValue(List<string> values, string response, string dataType) {
bool isFound = false;
switch (dataType) {
case "Date":
var parsedDate = DateTimeOffset.Parse(response).ToString("yyyy-MM-dd");
isFound = checkEquality(values, parsedDate);
break;
case "Time":
var parsedTime = DateTimeOffset.Parse(response).ToString("HH:mm:00.000");
isFound = checkEquality(values, parsedTime);
break;
}
return isFound;
}
The only real difference with these methods is that one property.这些方法的唯一真正区别是一个属性。 Is there a better approach I can use, so I don't have to repeat the repeating switch case code?
有没有更好的方法可以使用,所以我不必重复重复的 switch case 代码?
You already have a version that handles multiple values.您已经有一个可以处理多个值的版本。 Handling a single value is a special case of that so
处理单个值是一种特殊情况,因此
private bool FindMatchingValue(string value, string response, string dataType) {
bool isFound = false;
switch (dataType) {
case "Date":
var parsedDate = DateTimeOffset.Parse(response).ToString("yyyy-MM-dd");
isFound = checkEquality(value, parsedDate);
break;
case "Time":
var parsedTime = DateTimeOffset.Parse(response).ToString("HH:mm:00.000");
isFound = checkEquality(value, parsedTime);
break;
}
return isFound;
}
Could just be可能只是
private bool FindMatchingValue(string value, string response, string dataType) {
return FindMatchingValue(new List{ value }, response, dataType);
}
This way you don't need to duplicate the code, just provide an alternate method signature that can be called depending upon what is easier at the time.这样您就不需要复制代码,只需提供一个替代方法签名,可以根据当时更容易调用的方法来调用。 It's fairly common (in my experience) to see overloads like this all refer to a single method that has the necessary business logic.
看到像这样的重载都指的是具有必要业务逻辑的单个方法是相当普遍的(根据我的经验)。
Equally the List version could be replaced with a call to the original version of the single value同样,可以将 List 版本替换为对单个值的原始版本的调用
private bool FindMatchingValue(List<string> values, string response, string dataType) {
return values.Any(value => FindMatchingValue, response, dataType);
}
But the choice is yours.但选择权在你。
Use the list-version and flip the arguments around, and make it a params.使用列表版本并翻转参数,并使其成为参数。 It will now works for both.
它现在适用于两者。
private bool FindMatchingValue(string response, string dataType, params string[] values) {
bool isFound = false;
switch (dataType) {
case "Date":
var parsedDate = DateTimeOffset.Parse(response).ToString("yyyy-MM-dd");
isFound = checkEquality(values, parsedDate);
break;
case "Time":
var parsedTime = DateTimeOffset.Parse(response).ToString("HH:mm:00.000");
isFound = checkEquality(values, parsedTime);
break;
}
return isFound;
}
For the first method that only has the one string, you could do this:对于只有一个字符串的第一种方法,您可以这样做:
private bool FindMatchingValue(string value, string response, string dataType) => return FindMatchingValue(new List<String>{ value }, response, dataType)
It's a single line function that just calls the other这是一个单行函数,只调用另一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.