【问题标题】:In ETAS INCA, is there a way to quickly retrieve an item from an arbitrary path in an ASAP2Project?在 ETAS INCA 中,有没有办法从 ASAP2Project 中的任意路径快速检索项目?
【发布时间】:2013-08-15 09:57:38
【问题描述】:

我正在尝试通过 .NET API 连接到 INCA,以便导航 ASAP2 项目的文件夹结构。具体来说,我想获得一个代表我在下面突出显示的“目标”文件夹的对象。

这被证明是相当棘手的——API 提供了大量名称为“文件夹”的类:FolderAsap2ProjectFolderIncaFolder。那么,为了检索“目标”文件夹,我需要知道什么?

【问题讨论】:

    标签: .net extension-methods calibration


    【解决方案1】:

    您的第一个冲动可能是认为 Target 文件夹存在于像 USER A\Demo\Demo03\Foo\Bar\Baz\Target 这样的长路径上。不幸的是,INCA 没有这样做的机制——相反,您必须检索路径的 Database Objects 部分中的最低项目,然后查询该项目以获取 Datasets 路径的一部分。

    从数据库根目录,我们需要深入到Demo03,它是一个Asap2Project

    USER A\Demo\Demo03
    

    从 Demo03 中,我们需要向下钻取到 Target,这是一个 Asap2ProjectFolder

    Foo\Bar\Baz\Target
    

    第一部分非常简单——我们只需几行代码就可以连接到 INCA 并获得 Demo03 Asap2Project:

    Asap2ProjectFolder targetFolder = null;
    Inca myIncaInstance = new Inca();
    DataBase myDB = myIncaInstance.GetCurrentDataBase();
    DataBaseItem tempItem = myDB.GetItemInFolder("Demo03", "USER A\\Demo");
    
    if ((tempItem != null) && 
        (tempItem.IsAsap2Project()))
       {
       Asap2Project demoProject = (Asap2Project)tempItem;
    
       // Next step: grab the "Target folder!"
       }
    

    现在,事情变得有些棘手了。

    如果我们试图获得像 Demo03_1 这样的 DataSet,我们可以使用像 demoProject.GetDataSetForName("Demo03\\Demo03_1") 这样的调用。问题是,此方法仅适用于 DataSet 类型的对象。所以我们不能用它来检索像Foo\Bar\Baz\Target 这样的文件夹。如果您稍微研究过 API,您可能已经尝试过这样的事情:

    Asap2ProjectFolder tempProjFolder = demoProject.GetTopFolderNamed("Foo");
    tempItem = tempProjFolder.GetDataBaseItem("Bar\\Baz\\Target");  // Won't work.
    tempItem = tempProjFolder.GetSubFolder("Bar\\Baz\\Target");     // This won't either.
    

    事实证明,我们需要更加足智多谋。让我们为 Asap2ProjectAsap2ProjectFolder 类创建 GetItemInFolder() 扩展方法,这样这些类将具有与 DataBase 类相同的项目检索灵活性:

    namespace IncaExtensions
       {
       public static class GetItemInFolderExtensions
          {
          /// <summary>
          /// Gets a DataBaseItem from an Asap2Project in an arbitrary subfolder.
          /// </summary>
          /// <param name="project">The current Asap2Project</param>
          /// <param name="itemName">The name of the item that we want to retrieve</param>
          /// <param name="folderName">The path, delimited by backslashes ('\\')</param>
          /// <returns>A DataBaseItem matching the request.  
          /// If no matching item is found, or if the path is invalid, return null.
          /// </returns>
          public static DataBaseItem GetItemInFolder(this Asap2Project project, 
                                                     string itemName, 
                                                     string folderName)
             {
             DataBaseItem returnItem = null;
    
             if ((folderName != null) &&
                 (itemName != null))
                {
                string folderToken = PluckPathToken(ref folderName);
                Asap2ProjectFolder subFolder = 
                   project.GetTopFolderNamed(folderToken);
    
                if (subFolder != null)
                   {
                   returnItem = subFolder.GetItemInFolder(itemName, folderName); 
                   }
                }
    
             return returnItem;
             }
    
    
          /// <summary>
          /// Recursive call that returns a DataBaseItem in the target path.
          /// </summary>
          /// <param name="folder">The Asap2ProjectFolder to drill down</param>
          /// <param name="itemName">The name of the item that we want to retrieve</param>
          /// <param name="folderName">The path, delimited by backslashes ('\\')</param>
          /// <returns>A DataBaseItem matching the request.  
          /// If no matching item is found, or if the path is invalid, return null.
          /// </returns>
          public static DataBaseItem GetItemInFolder(this Asap2ProjectFolder folder, 
                                                     string itemName, 
                                                     string folderName)
             {
             DataBaseItem returnItem = null;
    
             if ((folderName != null) &&
                 (itemName != null))
                {
                string folderToken = PluckPathToken(ref folderName);
                Asap2ProjectFolder subFolder = 
                   (Asap2ProjectFolder)folder.GetSubFolder(folderToken);
    
                if (subFolder != null)
                   {
                   returnItem = subFolder.GetItemInFolder(itemName, folderName);
                   }
                }
             else
                {
                returnItem = folder.GetDataBaseItem(itemName);
                }
    
             return returnItem;
             }
    
    
          /// <summary>
          /// Removes a backslash-delimited token from a string and shortens the
          /// input string.
          /// </summary>
          /// <param name="folderName">Backslash-delimited path.  This will be 
          /// shortened each time a token is removed.</param>
          /// <returns>The latest path token</returns>
          static string PluckPathToken(ref string folderName)
             {
             int slashIdx = folderName.IndexOf('\\');
    
             // If folderName has path tokens, extract the first token and
             // shorten folderName accordingly.
             string folderToken = (slashIdx > -1) ? 
                folderName.Substring(0, slashIdx) : folderName;
             folderName = (slashIdx > -1) ? 
                folderName.Substring(slashIdx + 1) : null;
    
             return folderToken;
             }
          }
       }
    

    这使得从Asap2ProjectAsap2ProjectFolder 中获取项目变得非常容易。要获取 Target 文件夹,我们只需要以下调用:

    using IncaExtensions;
    ...
    Asap2ProjectFolder targetFolder = null;
    Inca myIncaInstance = new Inca();
    DataBase myDB = myIncaInstance.GetCurrentDataBase();
    DataBaseItem tempItem = myDB.GetItemInFolder("Demo03", "USER A\\Demo");
    
    if ((tempItem != null) && 
        (tempItem.IsAsap2Project()))
       {
       Asap2Project demoProject = (Asap2Project)tempItem;
       targetFolder = (Asap2ProjectFolder)demoProject.GetItemInFolder("Target", "Foo\\Bar\\Baz");
       }
    

    【讨论】:

    • 我已经创建了扩展方法的要点here
    猜你喜欢
    • 2023-04-05
    • 2020-06-20
    • 2017-01-20
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多