1

Classメソッド内に関数を実装しようとしていますが、C#は少し慣れていません。

基本的に、データベースの行を反復処理して値を変数に設定するメソッドがあります。次に、ドキュメントがすでに作成されている場合はドキュメントを更新し、作成されていない場合はドキュメントを作成します。コードの一部を除外しようとしていますが、変数への参照が必要なため、どこに配置すればよいかわかりません。ifelseステートメントで繰り返される項目を除外したいと思います。

    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

ルーチンをリファクタリングすることに加えて、タイピングを節約するためにいくつかの拡張メソッドを作成することをお勧めします。たとえば、doubleを解析するための拡張機能は次のとおりです。

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また、これらの一時変数は1回しか使用されないため、列からのすべての一時変数は必要ありません。それらを削除すると、メソッドがはるかに短くなり、読みやすくなります。

于 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 に答える