View RSS Feed

Development Team Blog

What is in a Name: Function Names Generated at Runtime

Rate this Entry
When writing the Migration Wizard I needed to come up with a solution for executing different routines depending on the revision the original workspace was from. As part of that solution, I decided to create a function that would generate the name of the routine on the fly, using the revision of the workspace that was being migrated. That way the main migration code would be simple and clean, and I would never need to change it in the future to accommodate upcoming revisions.

This is a simplified version of that function:
Code:
    Function RoutineName String sSWSFileName Returns String 
        String sRevision
        String sMigFunctionName
 
        // reads the revision from the passed SWS file
        Get OriginalRevision sSWSFileName to sRevision
        Move ("get_MigrateFrom" - trim(sRevision)) to sMigFunctionName
 
        Function_Return (replace(".", sMigFunctionName, "_"))
    End_Function

Then, for each revision, I created functions that according to each one of the original revisions would perform slightly different tasks.

This is what those functions look like:
Code:
 
    Function MigrateFrom14_1 String SWSToMigrate Returns Boolean
        Boolean bMigrationOK
 
        // whatever is required to migrate to 15.0
        Get Requirements15_0 SWSToMigrate to bMigrationOK
        If (not(bMigrationOK)) Begin
            Function_Return False
        End            
 
        Function_Return True
    End_Function    
 
    Function MigrateFrom14_0 String SWSToMigrate Returns Boolean
        Boolean bMigrationOK
 
        // whatever is required to migrate to 14.1
        Get Requirements14_1 SWSToMigrate to bMigrationOK
        If (not(bMigrationOK)) Begin
            Function_Return False
        End            
 
        // whatever is required to migrate to 15.0
        Get Requirements15_0 SWSToMigrate to bMigrationOK
        If (not(bMigrationOK)) Begin
            Function_Return False
        End                     
 
        Function_Return True
    End_Function

In the main migration routine, I use the following code to call the applicable routine during migration:
Code:
 
        String sMigrationFunctionName
        Integer iMsgId
        Boolean bMigrationOK
 
        // get function name
        Get RoutineName SWSToMigrate to sMigrationFunctionName
 
        // get message id
        Move (Eval(sMigrationFunctionName)) to iMsgId
 
        // execute it
        Get iMsgId SWSToMigrate to bMigrationOK
        If (not(bMigrationOK)) Begin
            Function_Return False
        End
So, if I am migrating a workspace from 14.0 to 15.0, RoutineName will return "get_MigrateFrom14_0". "Get iMsgId " will then execute what the function MigrateFrom14_0 includes. If I am migrating a workspace from 14.1 to 15.0, RoutineName will return "get_MigrateFrom14_1" and "Get iMsgId" will execute what the function MigrateFrom14_1 defines. If I am migrating a workspace from 12.1 to 15.0, well, you get the idea.

If you need to build something similar, say, for customers from different regions where the region determines what kind of actions your application needs to take, create your functions (e.g. CustomerFromRegion1, CustomerFromRegion2 and CustomerFromRegion3) and try this technique. I hope you will find it as useful as I did!

Updated 17-Aug-2009 at 08:44 AM by Stephen W. Meeley (Spell-check)

Categories
Uncategorized

Comments