Binding an access variable dynamically

You can use dynamic binding to access or update binding details in response to conditions at run time.

Here are two distinct tasks:

Dynamic binding is not available for access of a dedicated service from a Rich UI handler or a library.

Accepting the detail that is stored in a binding entry

Suppose that you created entries in the deployment descriptor file for two variations of the same service implementation:
<restBinding baseURI="http://myHostName" 
             enableGeneration="true" 
             name="MyEnglishBinding" 
             preserveRequestHeaders="false"/>

<restBinding baseURI="http://myHostName02" 
             enableGeneration="true" 
             name="MyFrenchBinding" 
             preserveRequestHeaders="false"/>

The first variation might return a value in English while the second a value in French.

You might create and bind two variables, one for each of these entries. Alternatively, you can create one variable that is based on the Interface part that corresponds to the service. Then, you can use the SysLib.getResource() system function to bind the variable to one or the other deployed service:

  1. Create a variable that is based on the Interface part:
    myService MyInterfacePart?;
  2. Use the getResource() function to bind the variable to the service implementation:
    myService = SysLib.getResource("MyEnglishBinding");
    In this case, the myService variable is now bound to the entry named MyBinding. If you avoid specifying an argument in the function call, the argument value is the name of the variable to which the function is assigning a value.
  3. Use the variable to access the service. This example is appropriate outside of Rich UI:
    myContent string = myService.myEcho("world");

    The value of myContent is “Hello, world!”

  4. You can use the getResource function again to bind the service to a different implementation:
    myTranslator = SysLib.getResource("MyFrenchBinding");
  5. Now you can use the variable to access the alternate service:
    myContent = myService.myEcho("world");

    The value of myContent is “Bonjour, monde!”

The effect of setting a Resource annotation is to generate code that is equivalent to the SysLib.getResource function invocation. Here are the reference topics:

Retrieving or creating the binding detail and updating it in memory

Here is example code, which prepares a variable that is later used in a service-invocation statement:
myService IMyService;    
myBinding HttpRest{@Resource};
myBinding.request.encoding = Encoding.json;
myService = servicelib.completeBind(myService, myBinding);
The code acts as follows:
  1. Declares an access variable:
    myService IMyService?;

    The Interface part typically includes a URI template annotation, which is a set of lower-level URI qualifiers that are resolved at run time. The resolved template might be /GetWeatherByZipCode?zipCode=27709.

  2. Accesses a new instance of an HTTPRest object that provides a higher-level URI such as www.example.com/myproject/restservices/weather_service. That object contains the details stored in a service binding; in this case, the service binding named myBinding.
  3. Adds detail to the HTTPRest object; for example, to ensure that data is transferred to and from the service in JSON format:
    myBinding.request.encoding = Encoding.json;
    
  4. Invokes the ServiceLib.completeBind function so that the variable references the HTTPRest object:
    myService = servicelib.completeBind(myService, myBinding);
The declaration of the binding object takes either of two forms:
  • One option is to retrieve an object that contains the data stored in the binding entry of the EGL deployment descriptor. Either of these two statements fulfills the need for HTTPRest:
    myBinding HttpRest{@Resource};
    
    // or
    myBinding HttpRest = SysLib.getResource("myBinding");

    In either case, the EGL runtime code accesses the deployment-descriptor entry named myBinding.

  • Alternatively, create a binding object in which you set all the fields of interest:
    http HttpRest = new HttpRest{
       restType = eglx.rest.ServiceType.TrueRest,	
       uri = "www.example.com/myproject/restservices/weather_service};