C#使用具有不同属性的多种类似方法的最佳实践?
我正在尝试简化和减少代码。
我有这样的方法:
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;
}
我有另一种方法,该方法列出了这样的字符串:
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;
}
这些方法的唯一真正区别是一个属性。 我可以使用更好的方法,因此我不必重复重复开关案例代码?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经有一个可以处理多个值的版本。处理单个值是一种特殊情况,因此
您不需要
复制代码,只需提供一个替代方法签名,可以根据当时更容易的方式称为。 (根据我的经验,看到类似的过载)都是相当普遍的,这都是指具有必要业务逻辑的单一方法。
同样,列表版本可以用单个值的原始版本替换,
但选择是您的。
You already have a version that handles multiple values. Handling a single value is a special case of that so
Could just be
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
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.
对于只有一个字符串的第一个方法,您可以执行此操作:
这是一个单行函数,只会调用另一个函数
For the first method that only has the one string, you could do this:
It's a single line function that just calls the other