Encapsulating your data model for reuse

Data modules simplify data access development in your applications. Data modules offer you a centralized design-time container for all your data access components. This enables you to modularize your code and separate the database access logic and business rules in your applications from the user interface logic in the application. You can also maintain control over the use of the data module by delivering only the .class files to application developers.

A data module is a specialized container for data access components. Once you define your DataSet components and their corresponding Column components in a data module, all frames that use the module have consistent access to the data sets and columns without requiring you to recreate them on every frame each time you need them. Data modules do not need to reside in the same directory or package as your project. They can be stored in a location for shared use among developers and applications.

The DataModule is an interface which declares the basic behavior of a data module. To work with this interface programmatically, implement it in your data module class and extend it by adding your data components.

Using the Data Module Wizard

To create a data module using the Data Module Wizard,

  1. Create a new project using the Project Wizard. You can create a data module in a separate project and once defined, refer to the data module class in your application. You can also create a data module in the same project as your application.
  2. Select File|New and double-click the Data Module icon to start the Data Module Wizard.
  3. Specify the package and class name for your data module class. JBuilder automatically fills in the Java file name and path based on your input.
  4. Click the OK button to close the dialog. The Data Module Wizard creates the class file and adds it to the project.

You'll notice that the code generated by the wizard for the data module class is slightly different than the code generated by other wizards. The getDataModule() method is defined as static public. The purpose of this method is to allow a single instance of this data module to be shared by multiple frames. The code generated for this method is:


static public DataModule1 getDataModule() {
    if (myDM == null)
      myDM = new DataModule1();
    return myDM;
  }
The code for this method This method is declared static so you can call this method without an instantiated DataModule.

The data module class now contains all the necessary methods for your custom data module class, and a method stub for the jbInit() to which you add your data components and custom business logic.

Adding data components to the data module

To customize your data module using the UI Designer, Note: JBuilder automatically creates the code for a public method that "gets" each DataSet component you place in the data module. This allows the DataSet components to appear as (read-only) properties of the DataModule. This also allows DataSet components to be visible to the dataSet property of data-aware components in the Component Inspector when data-aware component and data modules are used in the same container.

Adding business logic to the data module

Once the data components are added to the data module and corresponding properties set, you can add your custom business logic to the data model. To do so, you add code to various events of the DataSet components in the data module.

Note: The property settings and business logic code you add to the components in the data model cannot be overridden in the application that uses the data model. If you have behavior that you do not want to enforce across all applications that use this data model, consider creating multiple data models that are appropriate for groups of applications or users.

To add code to the events of a component,

When you're done creating and customizing your data module class, save and compile it. The next step is to reference the data module in your applications.

Using a custom data module in your application

In your application,

Click the Design tab to open the UI Designer; the instance of the data module appears in the Component Tree. Clicking the entry for the data module does not display its DataSet components nor its Column components. This is intentional to avoid modification of the business logic contained in the data module from outside.

When designing your application, you'll notice that the dataSet property of a UI Control includes all the DataSetView and StorageDataSet components that are included in your data module. You have access to them even though they are not listed separately in the Component Tree.

If you have a complex data model and /or business logic that you don't want another developer or user to manipulate, encapsulating it in a reusable component is an ideal way to provide access to the data but still enforce and control the business logic.