C#使用具有不同属性的多种类似方法的最佳实践?

发布于 2025-02-07 02:00:54 字数 1121 浏览 2 评论 0原文

我正在尝试简化和减少代码。

我有这样的方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2025-02-14 02:00:54

您已经有一个可以处理多个值的版本。处理单个值是一种特殊情况,因此

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(string value, string response, string dataType) {
    return FindMatchingValue(new List{ value }, response, dataType);
}

复制代码,只需提供一个替代方法签名,可以根据当时更容易的方式称为。 (根据我的经验,看到类似的过载)都是相当普遍的,这都是指具有必要业务逻辑的单一方法。

同样,列表版本可以用单个值的原始版本替换,

private bool FindMatchingValue(List<string> values, string response, string dataType) {
    return values.Any(value => FindMatchingValue, response, dataType);
}

但选择是您的。

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

private bool FindMatchingValue(List<string> values, string response, string dataType) {
    return values.Any(value => FindMatchingValue, response, dataType);
}

But the choice is yours.

宫墨修音 2025-02-14 02:00:54

使用列表反复并将参数翻转,并使其成为参数。现在它将对两者都有效。

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;
}

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;
}
讽刺将军 2025-02-14 02:00:54

对于只有一个字符串的第一个方法,您可以执行此操作:

private bool FindMatchingValue(string value, string response, string dataType) => return FindMatchingValue(new List<String>{ value }, response, dataType)

这是一个单行函数,只会调用另一个函数

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文