Partilhar via


Sugar-free C# – Part 2: Iterators 1

Task: as a breadth developer, I need to analyze a set of VB6 legacy applications and enlist all stored procedures invoked per subroutine per application.

Is this a task for a breadth developer? Let’s consider this criterion: if the number of concepts needed to successfully apply an effective solution to the problem exceeds what the developer could understand and apply in one day, then it is not a task for the breadth developer.

Let’s try C#’s iterators to scan a large set of VB6 legacy projects to group invoked stored procedures from each subroutine from each VB6 form and module. In our problem, all the VB6 projects’ code at hand has the same basic organization: classes, forms, and modules with subroutines from which stored procedures are invoked, the names for all stored procedures are in the code of each subroutine and start with the prefix “usp_”. The folder structure where all VB6 projects reside is a two-level only structure: a root folder with child folders for each VB6 project and its related files.

A sample of the required output is:

  • project1.vbp
    • module1.bas
      • subroutine1
        • usp_select1
        • usp_insert2
    • form1.frm
      • subroutine2
        • usp_select3
      • subroutine3
        • usp_delete4
        • usp_select5

 

With C# iterators we could return all the .vbp files scanning the root folder structure where the VB6 code is located. For each VB6 project file, we could use iterators to return all its forms and modules. From the text of each code file, we could use iterators to return all its subroutines. For each subroutine, we could use iterators to return all the names of stored procedures in it.

The overall structure for a C#’s iterators-based solution could be like this:

void f(DirectoryInfo VB6Root)

{

  foreach (DirectoryInfo folder in VB6Root.GetFolderHierarchy())

    foreach (FileInfo vbproject in folder.GetVB6Projects())

      foreach (FileInfo vbcodefile in vbproject.GetVB6CodeFiles())

        foreach (VB6Subroutine subroutine in vbcodefile.GetSubroutines())

          foreach (string storedprocedure in subroutine.GetInvokedStoredProcedures())

          {

            //...

          }

}

 

The definition for the methods in the structure above could be like this: to be continued...