Tutorial: Implementing a FireDAC EMS Client Application

From Appmethod Topics
Jump to: navigation, search

Go Up to EMS Client Application


You can create a client to connect to the EMS Server through the FireDAC Developer API that you previously set up using an EMS package.

This is a client sample application based on the resource created at Tutorial: Implementing a FireDAC EMS Resource.

Create a FireMonkey application and add a TDataModule to the project:

  • For Object Pascal
  1. File > New > Multi-Device Application - Object Pascal
  2. On the Project Manager, right-click Add New > Other > New Items.
  3. Go to Object Pascal Projects > Object Pascal Files > Data Module and click OK.
  • For C++:
  1. File > New > Multi-Device Application - C++
  2. On the Project Manager, right-click Add New > Other > New Items.
  3. Go to C++ Projects > C++ Files > Data Module and click OK.

On the TDataModule place the FireDAC components you need to proceed to the JSON data from the EMS Server.

Add the following components:

  • A TFDGUIxWaitCursor component.
  • A TFDSchemaAdapter component - to adapt the result of the JSON.
  • Two TFDTableAdapter components - to adapt the content of the two tables.
    • In the DatSTableName property set the name of the TFDQueries from the TDataModule. For this tutorial, use QEmployee and QCustomers.
    • In the SchemaAdapter property set the name of the TFDSchemaAdapter.
  • Two TFDMemTable components.
    • Set the names to mtEmployees and mtCustomers.
    • In the Adapter property set the name of the corresponding TFDTableAdapter.
    • Set the CachedUpdates property to True. You need to enable this option to update the info from the tables.
ClientModuleSample.png

In the Client Form include the TDataModule in the implementation section:

  • For Object Pascal:
implementation

{$R *.dfm}

uses Unit3;
  • For C++ (In the Unit1.cpp file):
#include "Unit2.h"

Add the following components to the client form:

  • Two TGrid components to show and update the data from the tables.
    • Go to View > LiveBindings Designer and connect the TGrid components with the TFDMemTable components. This step is necessary to show the data on the grid.
  • Two TButton components: one to get the data and the other to post the updates.
  • A TEMSProvider component to set the configuration to the EMS Server.
    • Set the URLHost to the IP of the server, localhost by default.
    • Set the URLPort to the port of the server, 8080 by default.
  • A TEMSFireDACClient to connect to the TFDSchemaAdapter from the client TDataModule.
    • Set the Provider property to the name of the TEMSProvider.
    • Set the Resource property to the name of the resource you want to access. For this tutorial, use FireDACTest.
    • Set the SchemaAdapter property to the name of the TFDSchemaAdapter from the TDataModule, for example ClientModule.FDSchemaAdapter2.
  • A TActionList component to define actions to get and update the data.
    • Right-click and select Action List Editor....
    • Create two new actions and change the name property of the actions to ActionGetTables and ActionPostUpdates.
    • Double-click the OnExecute event handler of the actions to create the procedures.

Write the code for the GetTablesExecute procedure:

  • For Object Pascal:
procedure TClientForm.ActionGetTablesExecute(Sender: TObject);
begin
  EMSFireDACClient1.GetData;
end;
  • For C++ (In the Unit1.cpp file):
void __fastcall TForm1::ActionGetTablesExecute(TObject *Sender) {
	EMSFireDACClient1->GetData();
}

Write the code for the PostUpdatesExecute procedure:

  • For Object Pascal:
procedure TClientForm.ActionPostUpdatesExecute(Sender: TObject);
begin
  EMSFireDACClient1.PostUpdates;
end;
  • For C++ (In the Unit1.cpp file):
void __fastcall TForm1::ActionPostUpdatesExecute(TObject *Sender) {
	EMSFireDACClient1->PostUpdates();
}
  • Define the Action property of the Get Tables button to ActionGetTables and the OnClick event handler to ActionGetTablesExecute.
  • Define the Action property of the Post Updates button to ActionPostUpdates and the OnClick event handler to ActionPostUpdatesExecute.

Testing the New EMS FireDAC Application

To test your project on a developer environment, make sure that the FireDAC EMS Resource is running on your EMS Server.

  1. To run your project, press F9 or choose Run > Run.
  2. Click the ActionGetTables button to retrieve the data from the database.
  3. Update any value and click ActionPostUpdates button to save the new data in the database.

See Also