Application DataModule

<< Click to Display Table of Contents >>

Navigation:  Technology Overview > Forms and Modules > DataModules >

Application DataModule

An application datamodule is a datamodule which gets additional support by the uniGUI run-time:

 

it is registered with the uniGUI run-time during the application initialization

it is created at the beginning of each session, or on-demand (according to the property MainModule.ApplicationDataModuleOptions.CreateOnDemand)

if not released by the user (using Free), it is automatically released when closing the session

 

AppDMOnDemand

 

Small applications requiring additional datamodules (that is, more than the MainModule), will probably use the default (CreateOnDemand = False), but big applications will enjoy better memory management creating the datamodules on-demand (and releasing them as soon as possible).

 

This is the auto-generated code for an Application DataModule:

 

unit _AppDM1;
 
interface
 
uses
  SysUtils, Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
  FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
  Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.Stan.StorageBin;
 
type
  TAppDM1 = class(TDataModule)
    tblList: TFDMemTable;
    tblListID: TAutoIncField;
    tblListText: TStringField;
    dsList: TDataSource;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
function AppDM1: TAppDM1;
 
implementation
 
{$R *.dfm}
 
uses
  UniGUIVars, uniGUIMainModule, MainModule;
 

// This function returns an instance of TAppDM1 datamodule.

// When MainModule.ApplicationDataModuleOptions.CreateOnDemand is true and datamodule instance is not created yet, 

// this function will create an instance and will return it otherwise it will return the previously created instance.

// When MainModule.ApplicationDataModuleOptions.CreateOnDemand is false this function will return same instance of datamodule which is created when session is started.
function AppDM1: TAppDM1;
begin
  Result := TAppDM1(UniMainModule.GetModuleInstance(TAppDM1));
end;
 
initialization
  RegisterModuleClass(TAppDM1);
 
end.

 

Notice how similar this code is to the Application Form.

 

If the parameter CreateOnDemand is changed to true, let's show how a form will use the application datamodule.

 

unit _TestForm;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm,
  _AppDM1, uniGUIBaseClasses, uniBasicGrid, uniDBGrid;
 
type
  TTestForm = class(TUniForm)
    UniDBGrid1: TUniDBGrid;
    procedure UniFormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
function TestForm: TTestForm;
 
implementation
 
{$R *.dfm}
 
uses
  MainModule, uniGUIApplication;
 
function TestForm: TTestForm;
begin
  Result := TTestForm(UniMainModule.GetFormInstance(TTestForm));
end;
 
procedure TTestForm.UniFormDestroy(Sender: TObject);
begin
  AppDM1.Free; // this code is optional. it can be used to free resources when associated form is freed and datamodule is no longer needed.

               // you should not call this method if datamodule is re-used several times or it is used by several other forms in the same session
               // if Free is not called here, datamodule will be freed when session is terminated.

end;
 
end.

 

The datamodule is explicitly released when destroying the form (that is, as soon as the form does not need it anymore). But there is no code linking the UniDBGrid to the datamodule. Let's take a look at the DFM file:

 

object TestForm: TTestForm
  Left = 0
  Top = 0
  ClientHeight = 246
  ClientWidth = 477
  Caption = 'Test (using App DM On Demand)'
  OldCreateOrder = False
  MonitoredKeys.Keys = <>
  OnDestroy = UniFormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object UniDBGrid1: TUniDBGrid
    Left = 0
    Top = 0
    Width = 477
    Height = 246
    Hint = ''
    DataSource = AppDM1.dsList
    LoadMask.Message = 'Loading data...'
    Align = alClient
    Anchors = [akLeft, akTop, akRight, akBottom]
    TabOrder = 0
  end
end

 

The link is also visible in the form designer:

 

AppDMOnDemand-Design-time

 

The datasource was linked at design-time to the datamodule. There is no need to do it at run-time.

 

Running the application proof that it is indeed working without memory leaks.

 

AppDMOnDemand-Run-time