Dynamic Lotusscript
Introduction
This short article gives an introduction to the underrated Execute method that is available in Lotusscript. The basics are explained through some simple examples, after which two suggestions are made for advanced usage.
The basics
The Execute method in Lotusscript is an underused feature that can be extremely powerful when used properly. This method allows you to dynamically load, execute, and unload Lotusscript modules. The syntax is extremely simple:
Execute ( text )
Where text is a string containing the script to execute. Here's a simple example:
Sub Initialize Dim strCode As String strCode = | msgbox "I'm dynamic!" | Execute( strCode ) End Sub
When you run this code, it will display a message box that says "I'm dynamic!". Now let's have a look at a dynamic script that spans multiple lines:
Sub Initialize Dim strCode As String strCode = | Dim strMessage as String strMessage = "I'm dynamic" msgbox strMessage | Execute( strCode ) End Sub
Note that pipeline characters are used to span multiple lines of Lotusscript into the strCode variable. This second example has exactly the same effect as the first, yet this time the dynamic code spans multiple lines and the message to prompt is defined in a variable.
The essence of the Execute method should be clear by now; one can dynamically run Lotusscript at runtime. The next two paragraphs demonstrate two interesting applications of this feature.
Dynamic class loading
Besides the simple series of statements from the previous examples, one can also include the Use and Declare keywords in the dynamic script. Consider the following script:
Sub Initialize Dim strCode as String strCode = | Use "libArrayManager" Dim AM as ArrayManager Set AM = new ArrayManager() | Execute strCode End Sub
This dynamic code loads a custom fictional class ArrayManager and instantiates a new ArrayManager object from it. The point in this example is that in line 4 we are dynamically loading a module. Thus, at runtime, we can programatically decide to load or not load a module. This technique is called Dynamic Class Loading. When applied correctly this technique can lead to performance gains, because only modules that are needed are actually loaded.
This is not a new technique, I have been using it successfully for years. The first time it appeared was in the must-read redbook Performance Considerations for Domino Applications. Bill Buchan has presented it at Lotussphere '05 as well, so I'll refer you to those two sources for the full details.
Web services
The second advanced application of the Execute method comes quite close to Web Services. One of the key concepts of a web service is that you can dynamically call functions (services) and retrieve the return values, if any. The other concept is that you can do this from anywhere, independant of your calling client, OS or hardware.
Allow me to demonstrate how you can realize the first concept through a simple example:
Declarations Dim strReturnVal as String Sub Initialize Dim strCode as String Dim strModule as String Dim strFunction as String Dim strParam as String
strModule = "libUser" strFunction = "getCountry" strParam = "Orky Dorky"
strCode = | Use | & strModule & | strReturnVal = | & strFunction & |(| & strParam & |)| Execute strCode Msgbox strReturnVal End Sub
Look at that. Everything is dynamic, the module to load, the function to execute, and the parameter to pass on. Pay particular attention to the bold line in the example. Key concept here is that we can communicate between our static and dynamic script through variables. Note that this only works for globally declared variables, that's why strReturnVal is declared in the top line of the example.
So how does this come close to a web service? We have proven that we can call a function and pass parameters based on data. In the example, this data is defined in variables, but they might have been coming from a SOAP message, email, or Notes document.
Of course, the example is too simple. Not all functions have a return value. Not all functions have exactly one parameter of that data type. Just think of the base concept, the road for web services is paved. Available from Notes 4.6, go figure!
» Excellent .Really usefull «