【问题标题】:C# function nested within a class method嵌套在类方法中的 C# 函数
【发布时间】:2012-09-11 04:54:35
【问题描述】:

我正在尝试在 Class 方法中实现一个函数,但我对 C# 有点陌生。

基本上,我有一个方法可以遍历数据库中的行并将值设置为变量。然后,如果已创建文档,则更新该文档,否则创建该文档。我试图排除一些代码,但我不知道把它放在哪里,因为它仍然需要引用我的变量。我想将 if else 语句中的重复项分解出来。

    private void SyncKinases()
    {
        DataSet ds = new DataSet();
        ds = gn.ExecuteQuery("dx.kinasedatasheet.selectstagingkinases", null);

        TreeProvider tp = new TreeProvider(ui);
        VersionManager vm = new VersionManager(tp);
        TreeNode node;

        WorkflowManager wm = new WorkflowManager(tp);

        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string className = "dx.kinasedatasheet";
                string title = dr["Title"].ToString();
                string technologyPlatform = dr["TechnologyPlatform"].ToString();
                string ambitGeneSymbol = dr["AmbitGeneSymbol"].ToString();
                string targetDescription = dr["TargetDescription"].ToString();
                string entrezGeneSymbol = dr["EntrezGeneSymbol"].ToString();
                int entrezGeneID = int.Parse(dr["EntrezGeneID"].ToString());
                string aliases = dr["Aliases"].ToString();
                string kinaseGroup = dr["KinaseGroup"].ToString();
                string species = dr["Species"].ToString();
                string accessionNumber = dr["AccessionNumber"].ToString();
                string kinaseConstruct = dr["KinaseConstruct"].ToString();
                string kinaseForm = dr["KinaseForm"].ToString();
                string expressionSystem = dr["ExpressionSystem"].ToString();
                double avgZValue = 0;
                if (!(dr["AverageZValue"] is DBNull))
                {
                    avgZValue = double.Parse(dr["AverageZValue"].ToString());
                }
                string panel = dr["Panel"].ToString();
                string compoundsKds = dr["CompoundsKds"].ToString();
                string mutationRelevance = dr["MutationRelevance"].ToString();
                string mutationReferences = dr["MutationReferences"].ToString();

                string kinaseAliasPath = "/Kinase-Data-Sheets";

                if (!(dr["NodeID"] is System.DBNull))
                {
                    node = tp.SelectSingleNode(int.Parse(dr["NodeID"].ToString()));
                    vm.CheckOut(node);
                    node.DocumentName = ambitGeneSymbol;
                    node.NodeName = ambitGeneSymbol;
                    node.SetValue("Title", title);
                    node.SetValue("TechnologyPlatform", technologyPlatform);
                    node.SetValue("AmbitGeneSymbol", ambitGeneSymbol);
                    node.SetValue("TargetDescription", targetDescription);
                    node.SetValue("EntrezGeneSymbol", entrezGeneSymbol);
                    node.SetValue("EntrezGeneID", entrezGeneID);
                    node.SetValue("Aliases", aliases);
                    node.SetValue("KinaseGroup", kinaseGroup);
                    node.SetValue("Species", species);
                    node.SetValue("AccessionNumber", accessionNumber);
                    node.SetValue("KinaseConstruct", kinaseConstruct);
                    node.SetValue("KinaseForm", kinaseForm);
                    node.SetValue("ExpressionSystem", expressionSystem);
                    if (!(dr["AverageZValue"] is DBNull))
                    {
                        node.SetValue("AverageZValue", avgZValue);
                    }
                    node.SetValue("Panel", panel);
                    node.SetValue("CompoundsKds", compoundsKds);
                    node.SetValue("MutationRelevance", mutationRelevance);
                    node.SetValue("MutationReferences", mutationReferences);
                    node.SetValue("DocumentPublishTo", null);

                    node.Update();
                    updatedKinaseCount++;
                    vm.CheckIn(node, null, null);
                    WorkflowInfo wi = wm.GetNodeWorkflow(node);
                    if (node.IsPublished)
                    {
                        wm.AutomaticallyPublish(node, wi, null);
                    }
                }
                else
                {
                    node = TreeNode.New(className, tp);
                    node.DocumentName = ambitGeneSymbol;
                    node.NodeName = ambitGeneSymbol;
                    node.SetValue("Title", title);
                    node.SetValue("TechnologyPlatform", technologyPlatform);
                    node.SetValue("AmbitGeneSymbol", ambitGeneSymbol);
                    node.SetValue("TargetDescription", targetDescription);
                    node.SetValue("EntrezGeneSymbol", entrezGeneSymbol);
                    node.SetValue("EntrezGeneID", entrezGeneID);
                    node.SetValue("Aliases", aliases);
                    node.SetValue("KinaseGroup", kinaseGroup);
                    node.SetValue("Species", species);
                    node.SetValue("AccessionNumber", accessionNumber);
                    node.SetValue("KinaseConstruct", kinaseConstruct);
                    node.SetValue("KinaseForm", kinaseForm);
                    node.SetValue("ExpressionSystem", expressionSystem);
                    if (!(dr["AverageZValue"] is DBNull))
                    {
                        node.SetValue("AverageZValue", avgZValue);
                    }
                    node.SetValue("Panel", panel);
                    node.SetValue("CompoundsKds", compoundsKds);
                    node.SetValue("MutationRelevance", mutationRelevance);
                    node.SetValue("MutationReferences", mutationReferences);
                    node.SetValue("DocumentPublishTo", null);
                    node.SetValue("DocumentCulture", "en-US");

                    TreeNode parentNode = tp.SelectSingleNode("DiscoveRx", kinaseAliasPath, "en-US");
                    node.Insert(parentNode);
                    //vm.CheckIn(node, null, null);
                    newKinaseCount++;
                }
            }
        }
        ArchiveItems(archivedKinaseCount, "dx.kinasedatasheet.selectarchivekinases");
    }

【问题讨论】:

    标签: c# function methods nested


    【解决方案1】:

    除了重构您的例程之外,我还建议您创建一些扩展方法来节省您的打字时间。例如,这是解析双打的扩展:

    public static class Extensions
    {
        public static double ToDoubleIfNotDBNull(this object item)
        {
           if (item is DBNULL) return 0;
           return double.Parse(item.ToString());
        }
    }
    

    那么你的代码就变成了:

    double avgZValue = dr["AverageZValue"].ToDoubleIfNotDBNull();
    

    【讨论】:

      【解决方案2】:

      你可以重构你的代码,这样你就不需要在不同的情况下设置节点值:

      private void SyncKinases()
      {
          DataSet ds = new DataSet();
          ds = gn.ExecuteQuery("dx.kinasedatasheet.selectstagingkinases", null);
      
          TreeProvider tp = new TreeProvider(ui);
          VersionManager vm = new VersionManager(tp);
          TreeNode node;
      
          WorkflowManager wm = new WorkflowManager(tp);
      
          if (ds.Tables[0].Rows.Count > 0)
          {
              foreach (DataRow dr in ds.Tables[0].Rows)
              {
                  string className = "dx.kinasedatasheet";
                  string title = dr["Title"].ToString();
                  string technologyPlatform = dr["TechnologyPlatform"].ToString();
                  string ambitGeneSymbol = dr["AmbitGeneSymbol"].ToString();
                  string targetDescription = dr["TargetDescription"].ToString();
                  string entrezGeneSymbol = dr["EntrezGeneSymbol"].ToString();
                  int entrezGeneID = int.Parse(dr["EntrezGeneID"].ToString());
                  string aliases = dr["Aliases"].ToString();
                  string kinaseGroup = dr["KinaseGroup"].ToString();
                  string species = dr["Species"].ToString();
                  string accessionNumber = dr["AccessionNumber"].ToString();
                  string kinaseConstruct = dr["KinaseConstruct"].ToString();
                  string kinaseForm = dr["KinaseForm"].ToString();
                  string expressionSystem = dr["ExpressionSystem"].ToString();
                  double avgZValue = 0;
                  if (!(dr["AverageZValue"] is DBNull))
                  {
                      avgZValue = double.Parse(dr["AverageZValue"].ToString());
                  }
                  string panel = dr["Panel"].ToString();
                  string compoundsKds = dr["CompoundsKds"].ToString();
                  string mutationRelevance = dr["MutationRelevance"].ToString();
                  string mutationReferences = dr["MutationReferences"].ToString();
      
                  string kinaseAliasPath = "/Kinase-Data-Sheets";
      
                  bool isNewNode = false;
                  if (!(dr["NodeID"] is System.DBNull))
                  {
                      node = tp.SelectSingleNode(int.Parse(dr["NodeID"].ToString()));
                      vm.CheckOut(node);
      
                  }
                  else
                  {
                      node = TreeNode.New(className, tp);
                      node.SetValue("DocumentCulture", "en-US");
                      isNewNewNode = true;
                  }
      
                  node.DocumentName = ambitGeneSymbol;
                  node.NodeName = ambitGeneSymbol;
                  node.SetValue("Title", title);
                  node.SetValue("TechnologyPlatform", technologyPlatform);
                  node.SetValue("AmbitGeneSymbol", ambitGeneSymbol);
                  node.SetValue("TargetDescription", targetDescription);
                  node.SetValue("EntrezGeneSymbol", entrezGeneSymbol);
                  node.SetValue("EntrezGeneID", entrezGeneID);
                  node.SetValue("Aliases", aliases);
                  node.SetValue("KinaseGroup", kinaseGroup);
                  node.SetValue("Species", species);
                  node.SetValue("AccessionNumber", accessionNumber);
                  node.SetValue("KinaseConstruct", kinaseConstruct);
                  node.SetValue("KinaseForm", kinaseForm);
                  node.SetValue("ExpressionSystem", expressionSystem);
                  if (!(dr["AverageZValue"] is DBNull))
                  {
                      node.SetValue("AverageZValue", avgZValue);
                  }
                  node.SetValue("Panel", panel);
                  node.SetValue("CompoundsKds", compoundsKds);
                  node.SetValue("MutationRelevance", mutationRelevance);
                  node.SetValue("MutationReferences", mutationReferences);
                  node.SetValue("DocumentPublishTo", null);
      
                  if (isNewNode)
                  {
                      TreeNode parentNode = tp.SelectSingleNode("DiscoveRx", kinaseAliasPath, "en-US");
                      node.Insert(parentNode);
                      //vm.CheckIn(node, null, null);
                      newKinaseCount++;
      
                  }
                  else
                  {
                      node.Update();
                      updatedKinaseCount++;
                      vm.CheckIn(node, null, null);
                      WorkflowInfo wi = wm.GetNodeWorkflow(node);
                      if (node.IsPublished)
                      {
                          wm.AutomaticallyPublish(node, wi, null);
                      }
                  }
              }
          }
          ArchiveItems(archivedKinaseCount, "dx.kinasedatasheet.selectarchivekinases");
      }
      

      您现在也不需要来自dr 列的所有这些临时变量,因为它们只会被使用一次。删除这些将使您的方法更短且更具可读性。

      【讨论】:

      • 所以你的意思是我可以这样做:node.SetValue("Title", dr["Title"].ToString();)?
      • 这很棒。我不是这么想的。谢谢。
      【解决方案3】:

      只需创建一个新方法并将值作为参数发送。

      void SetNodeValues(Node node, DataRow row)
      {
          string title = dr["Title"].ToString();                
          ....
      
          node.SetValue("Title", title);
          ...
      }
      

      您也许可以使用 for 循环完成所有操作(未经测试且与您的变量不匹配)

      foreach(var col in Table.Columns)
          node.SetValue(col.Name, dr[col]);
      

      如果您使用的是 ORM,则可以发送一个对象而不是 DataRow,但这超出了本问题的范围。

      【讨论】:

        猜你喜欢
        • 2017-07-29
        • 1970-01-01
        • 2021-12-30
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-07
        相关资源
        最近更新 更多