简体   繁体   English

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

[英]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.

相关问题 C#:扩展方法和Not运算符最佳实践 - C#: Extension methods and the Not operator Best Practice 避免在C#中使用`using`关键字进行多次处置的最佳实践 - Best practice to avoid multiple disposals with the `using` keyword in C# 在C#中命名私有和静态私有方法的最佳实践是什么? - What is the best practice for naming private and static private methods in C#? 在类中初始化变量的最佳实践,可用于 C# 中的所有方法 - Best practice to initialize variable in classes, accessible for all methods in C# C#:在扩展方法中验证“ this”参数的最佳实践 - C#: Best practice for validating “this” argument in extension methods C#IDisposable使用:最佳实践 - C# IDisposable Using: Best Practice 在 C# 中使用“out”关键字的最佳实践 - Best practice of using the “out” keyword in C# 比较C#彩票中的对象属性的最佳实践 - Best practice to compare Object properties in C# Lottery Ticket 为什么将文件上传到 Slack 的两种(几乎)相似的方法(使用 c#)会导致不同的结果? - Why do two (almost) similar methods(using c#) of uploading a file to Slack cause different outcomes? 最佳实践:访问器属性或无参数方法? - Best practice: Accessor properties or parameterless methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM