【问题标题】:Avoiding repetitive method implementation in C#避免在 C# 中重复方法实现
【发布时间】:2017-01-20 13:29:34
【问题描述】:

我有几个类,我需要解析 json 对象。我看到这个 json 对象的初始循环在所有类中都几乎相同,除了子方法。

例如在Class1.cs中

private static void FindObject(JToken token)
{
  switch (token.Type)
  {
      case JTokenType.Array:          
         JArray array = token as JArray;
         array.ForEach(a => FindObject(a));
         break;           
      case JTokenType.String:
         token.Replace(GetNewImgTag(token.ToString()));
         break;
      case JTokenType.Object:
         token.Children().ForEach(t => FindObject(t));
         break;
       case JTokenType.Property:
         JProperty prop = token as JProperty;

         if (prop.Value.Type == JTokenType.Array)
         {
             FindObject(prop.Value);
             return;
         }

         prop.Value = GetNewImgTag(prop.Value.ToString());
         break;
      default:
         throw new NotImplementedException(token.Type + " is not defined");
  }    
}

private static JToken GetNewImgTag(string text)
{
   ...
}

和 Class 2.cs 是

private static void FindObject(JToken token)
{
  switch (token.Type)
  {
      case JTokenType.Array:          
         JArray array = token as JArray;
         array.ForEach(a => FindObject(a));
         break;           
      case JTokenType.String:
         token.Replace(ReplaceLinks(token.ToString()));
         break;
      case JTokenType.Object:
         token.Children().ForEach(t => FindObject(t));
         break;
       case JTokenType.Property:
         JProperty prop = token as JProperty;

         if (prop.Value.Type == JTokenType.Array)
         {
             FindObject(prop.Value);
             return;
         }

         prop.Value = ReplaceLinks(prop.Value.ToString());
         break;
      default:
         throw new NotImplementedException(token.Type + " is not defined");
  }    
}

private static JToken ReplaceLinks(string text)
{
   ...
}

如果你比较这两个类,FindObject() 几乎相同,除了子方法调用。我需要在几个班级中实现这一点。我正在尝试避免这种多重重复方法的创建。

谁能提出更好的设计方法?

我在这里看到了类似的帖子,但我无法将此代表应用于我的场景。

Avoiding repetitive code in multiple similar methods (C#)

【问题讨论】:

  • 为什么不能申请委托。该方法将更改为FindObject(JToken token, Func<string,JToken> getValue) 并应用prop.Value = getValue(prop.Value.ToString());
  • 对于case JTokenType.Property:,为什么不检查该值是否为字符串?它可以是整数或嵌套对象或日期等。

标签: c# generics design-patterns delegates json.net


【解决方案1】:

一种直接的方法是识别不同的部分,并将其作为您传递给单独函数的委托。

这是一个工作示例。

public static class MyTokenReaderUtilities
{
     public static void ConvertEachProperty(JToken token, Func<string, JToken> convertString)
     {
        switch (token.Type)
        {
            case JTokenType.Array:          
               JArray array = token as JArray;
              array.ForEach(a => ConvertEachProperty(a, convertString));
              break;           
           case JTokenType.String:
             token.Replace(convertString(token.ToString()));
           break;
          case JTokenType.Object:
              token.Children().ForEach(t => ConvertEachProperty(t, convertString));
              break;
          case JTokenType.Property:
              JProperty prop = token as JProperty;

              if (prop.Value.Type == JTokenType.Array)
              {
                ConvertEachProperty(prop.Value, convertString);
                return;
              }
              prop.Value = convertString(prop.Value.ToString());
              break;
          default:
             throw new NotImplementedException(token.Type + " is not defined");
       }  
     }
}

现在,在第 1 课中:

private static void FindObject(JToken token)
{
   MyTokenReaderUtilities.ConvertEachProperty(token, GetNewImgTag);
}

在第 2 课中:

private static void FindObject(JToken token)
{
   MyTokenReaderUtilities.ConvertEachProperty(token, ReplaceLinks);
}

【讨论】:

  • 看起来像重构主题,因此我建议来自 Martin Fowler 的book
猜你喜欢
  • 2015-12-18
  • 2013-06-21
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
相关资源
最近更新 更多