Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EDT:Resource Binding Introduction

Revision as of 17:26, 19 January 2012 by Unnamed Poltroon (Talk)

Introduction for Resource binding

One of the more elegant aspects of EGL is its use of resource bindings, each of which is a value that describes how to access a service or database. In most cases, you maintain bindings in an EGL deployment descriptor, which is external to your logic and provides access details when you are developing or deploying your application.

This use of the deployment descriptor is safe and flexible. You can change the details stored there and redeploy the code without changing the logic and without spending the time to regenerate output.

The binding mechanism is the same for service and database access. The typical process is to write a resource binding in an EGL deployment descriptor and to relate a variable to the stored resource binding as follows:

  • By invoking the SysLib.getResource function; or
  • By writing a Resource annotation.

Here is an example use of the function, which can be invoked only inside an EGL function:

myService MyInterfaceType?;

myService = SysLib.getResource("MyBinding"); 

Here is the equivalent, simpler annotation, which you can specify anywhere you can declare a variable:

myService MyInterfaceType?{@Resource{bindingKey = "myBinding"}};

In either case, access to the stored binding occurs at run time, when the generated output invokes code that is equivalent to the EGL SysLib.getResource function.

A resource binding includes a series of fields that is characteristic of a particular type of binding. For example, a REST service binding has fields that are different from those in a database binding. The existence of binding types means that you can go beyond the typical process described earlier:

  • You might define a variable that is of the appropriate binding type. You can assign field values to that variable and use the variable for resource access. In this case, the resource binding is solely in your code.
  • In relation to service bindings, you can initialize the variable with values from the EGL deployment descriptor and then update the fields in your code.

The next sections give further details on all these capabilities.

Binding DB with Reference in workspace

Add Database Binding

Open the egl deploy description file ***.elgdd and switch to “Resource Bindings” Tab, you will find there is a list for all the Resource binding of this project.


Img1.JPG


You can add new database bind by click the Add button. After clicked the Add button, a “Add a Resource Binding” dialog will pop and please check the Radio button of “SQL database binding” then click “Next” button

Bind Img2.JPG


Select database biding type

In the “Add a SQL Database Binding” dialog you can see, there are two types of SQL Binding

  • Reference the selected workspace connection below(retrieved at runtime)
  • Add the information from the selected connection below(hard-coded information)

There are differences between these two types of DB bindings. If you selected Reference binding, the DB information will be stored in deploy descriptor and you can update or edit it anytime via the egldd file without to touch the source code in order that to avoid the potential risk.

Select type of Reference the selected workspace connection below (retrieved at runtime)

Bind Img3.JPG


Select a database connection from the list or you can follow the instruction EDT:Tutorial:_Access_a_database_with_EGL_Rich_UI_Lesson_3 to create a database connection for your project.
Click the “Finish” button to complete the database connection binding. The database binding will be completed.

Bind img4.JPG


This type of database binding is retrieved at runtime, the database information depends on the database connection in this project. If you edited the database connection information, the database binding will also change accordingly.

Using the database binding in code

In project you can use this database binding to define a SQLDataSource.
For example:


//Define the SQLDataSource
ds SQLDataSource?{@resource{propertyFileName = "crud" , bindingkey = "NewMySQL"}};
or
ds SQLDataSource = SysLib.getResource("NewMySQL");
// Function Declarations
function getTable() returns (expense[])
    exp expense[];
    get exp from ds;
    return (exp);
end

“propertyFileName” is the egldd file name of the project.
“bindingkey” is the SQL DataBase Binding Name which you created in 2.2

Binding DB with hard-coded

Add database binding

Please refer to section 2.1 for the detail<span style="text-decoration: underline;" />

Select database binding type

Select type of Add the information from the selected connection below(hard-coded information). You can also use the existing database connection or create a new database connection. The completed database binding should like below:

Bind Img5.JPG


In can also update or exit the database information manually. After updated database information manually, do NOT forget to click the “Test Connection” to verify the database connection worked.

Using the database binding in code

Please refer to section 2.3

Other way to define SQL database connection

Besides the resource binding, we can also define the SQL database connection directly in our source code.
For example:

// the data source includes a connection string, as well as
// security details that are stored in a dictionary.
 
connectURL string = "jdbc:derby:SomeDB;create=true;";
properties Dictionary{user = "MyID", password = "MyPassword"};
ds SQLDataSource? = new SQLDataSource(connectURL, properties);

How to binding a service in EDT

There are two type of service binding in EDT current.

  • @Resource
  • @DedicatedService

In this section. You can learn how to binding a service in your project by these two types of binding.
Firstly, we should assume that we have already defined a service named: myService.

Binding service @Resource

In order to binding the service via @Resource, you should add the service into the Services Deployment.
Open the egldd file and switch to “Service Deployment” tab. Then click the Add button.

Bind Img6.JPG

You can add the service myService into your Services Deployment setting up.

Bind Img7.JPG


Then switch to Resource Bindings tab to add the REST service binding. Click Add button and select “Rest Service Binding” to add the service binding.

Bind Img8.JPG

You should give a name for your service binding. For the Base URI you can use two types of URI as the instruction in the dialog.

  • Deployed URI.

              Myhostname:8080 is your project server’s host and port number.

              “myTargetWebProject” is the project name which you want to deploy into. You can find the name in “Overview” tab of the elgdd file

              “restservices”. Fixed string. Please do NOT modify it

              “myService” : your service’s name.

  • Workspace URI

              “myServiceProject” : your project’s name.
              “myPackage.myservice”: your service’s name and its project’s name

Click Finish to complete the binding.

Use the service binding in code

In source code you can use the service binding your defined in section 3.1 as below:



mysrv  mysrv ?{@Resource {}};
or
mysrv mysrv?{ @Resource { propertyFileName = “egldd file name” , bindingkey = “ REST service name”}}

Then you can call the methods in myService


Binding service @DedicatedService

This type of service binding is simple and did not need to setup in elgdd file. You can just code in your source as below:
mySrv myService?{@DedicatedService {}};
or
mySrv myService?{@DedicatedService { serviceName = “service name” }};


Then you can use the methods you defined in service: myService.

Back to the top