1

我正在尝试在 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");
    }
4

3 回答 3

2

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

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();
于 2012-09-18T21:49:31.310 回答
1

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

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列中的所有这些临时变量,因为它们只会使用一次。删除这些将使您的方法更短且更具可读性。

于 2012-09-18T21:30:03.057 回答
0

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

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,但这超出了此问题的范围。

于 2012-09-18T21:26:33.397 回答