文件名称:
Tutorial - Using a REST DataSnap Server with an Application and FireDAC (draft copy).pdf
开发工具:
文件大小: 614kb
下载次数: 0
上传时间: 2019-07-27
详细说明:Tutorial - Using a REST DataSnap Server with an Application and FireDAC (draft copy).pdf4. Save the FormUnit1 as serverUnit, the webModuleUnit1 as Webmodule Unit 1
the serverMethodsUnit1 as Server/ethodsUnit1, and save the project as My serverProj
Key data Snap Server Components
When you create the Data Snap REST Application, all the Data Snap components needed are
automatically added to the WebModuleUnit 1 uni
■
DSServer 1.
DSServer Class1
DshtTpweBdispAtcHer1
a
WebFileDispatcher1
封
ServerFunctionInvoker.: ReverseString
DSProxy Generator 1
DSServerMetaDataProvider 1
The main components on the webModuleUnit1 unit are
■ TDSServer
TDSServer Class
The TDSServer component is the logical heart of the data Snap server application. It contains
the Start and Stop methods for starting and stopping the server You need only
one TDSServer component per server application
The TDSServer Class component represents a server class. The Data Snap server automatically
creates and destroys instances of server classes
Thehttpcommunicationprotocolprovidesthecommunicationbetweentheclientandtheserver
Adding Fire DAC components to the Server Module
Add the following components to the ServerMethodsUnit1 unit
A TFDConnection component Set its Name property to " FDConnectionEMPLOYEE
Right-click the component and click Connection Editor. The Fire Dac Connection
Editor window opens where you have to define the connection parameters
Select IB from the drop-down menu for the Driver ID field
Set the path to the interbase databas
C: Users\Public\Documents EmbarcaderolStudio\15.0\ Samples\Data EMPLOYEE.GDB
Set the User Name and Password. The values by default for these parameters are
User Name= sysdba and Password= masterkey
Click OK to save the changes
On the Object Inspector change the Login Prompt property to False
Set the Connected property to True
A TFDPhysl BDriverLink component to connect to an Inter Base database
A TFDGUlxWaitCursor component
Three TFDQuery components
Right-click the TFDQuery component and click Query Editor to open the FireDAC Query
Editor window to introduce the sql statements as detailed later
Rename one component to FDQuery DepartmentNames and Introduce the following SQL
statement: select dept no, department from department and click OK. this query returns
the list of department numbers and names. the result of this query is used at the client to
build the list of departments. In order to retrieve this information the client app needs to cal
the GetDepartmentNames"server method
Rename the second component to FDQuery Department and introduce the following SQL
statement: select from department where dEPt No DEPT and click OK
Rename the last component to FDQuery Department Employees and introduce the
following statement: select from employee where dept no=: DEPT and click OK
Note: The last two queries-FDQuery Department and FDQuery DepartmentEmployees-are
used to retrieve more detailed information for a given department from DEPARTMENT and from
EMPLOYEE tables. The information returned from both queries is exposed to clients via
GetDepartmentEmployees" server method
A TFDStan StorageJsoNLink component
A TFDStan Storage Bin Link component
4
The serverMethodsUnit1 contains the implementation of two simple methods
called EchoString and Reverse String, which return the value given as a parameter in normal
respective reversed states. These are just example methods
In this example, we add new methods to ServerMethodsUnit1 to retrieve JSoN data from the
underlying Inter Base EMPLOYEE sample database, and the server exposes these methods to the
client
GetDepartmentNames" server method
In delphi
This method gets all departments and give a TFDjSONData Sets as a result
SMETHODINFO ONA
public
Public declarations
function GetDepartmentNames: TFDUSONDatasetsi
(SMETHODINFO OFF
Use class completion by pressing CTRL-SHIFT-C to create a stub for this function in
the implementation section.
Write code for the function you just added
//GetDepartmentNames
function TServerMethodsl GetDepartmentNames: TFDUSONDataSets;
begi
// Clear active
ha七
11 reexecute
FDQueryDepartmentNames. Active : False;
Result : TFDJSONDataSets create;
TFDUSONDatasetsWriter. ListAdd(Resultr
FDQuery DepartmentNames)i// The "TFDJSONDataSetswriter class
provides static ListAdd" method that is using reflection to
convert results of the query into TFDUSONDatasets
end
Note: The( SMETHCDINFO ON directive causes the generation of run-time information
needed by the Datasnap server, so this is required--it is not just a comment
See METHODINFO directive(Delphi) for more information
The function returns a new data type: TFDJSONDataSets To use this type you need to include a
new unit in the uses section data Fire DACjSoNReflect
5
In c++
This method gets all departments and give a TUSONObject as a resu
ublic:
// User declarations
TUSONObject* GetDepartmentNames()i
TUSONObject* TServerMethods1:: GetDepartmentNames(
FDQueryDepartmentNames->Close()i
TFDUSONDataSets *ds new TFDUSONDataSetso
TFDJSONDataSetsWriter:: ListAdd(ds
FDQueryDepartmentNames)i
TUSONObject *ob]= new TUSoNObject()i
TFDUSONInterceptor: DataSetsTouSoNObject(ds, ob])
return ohji
You need to include these units
1. include"System. Json. hpp"l/TJSoNObject
2. include"Data. Fire DACJSONReflect hpp"//TFDJSONData Sets
GetDepartment Employees"server method
Use this method to retrieve more detailed information for a given department from DEPARTMENT
and from EMPLOYEE tables using FDQueryDepartment and FDQueryDepartmentEmployees. We
can get the data from both queries using just one method For this purpose we need to create two
constants
In Delphi
//Include the constants under the uses section of the
implementation
const
dEpartment =Department i
eMployees =Employees i
( SMETHODINFO ON)
public
I Public declarations
function GetDepartmentEmployees(const AID: string)
TFDUSONDatasetsi
6
I SMETHODINFO OFF)
Use class completion by pressing CTRI-SHIFT-C to create a stub for this function in
the implementation section
Write code for the function you just added
function TServerMethodsl GetDepartmentEmployees (const AID:
string): TFDUSONDatasets;
begin
// Clear active so that query will reexecute
FDQuery DepartmentEmployees Active :-Falsei
FDQueryDepartment. Active :False;
FDQuery Department Params [O] Value :=AID
FDQuery DepartmentEmployees. Params [o]Value :=AID
/ Create dataset list
Result := TEDJSONDatasets create;
// Add departments dataset
TFDJSONDatasetsWriter. ListAdd(Result, dEpartment,
FDQueryDepartment)i
Add employees dataset
TFDJSONDataSetsWriter.ListAdd(result, eMployees,
FDQuery DepartmentEmployees)
end:
These two methods are used by a client app to receive information about departments. first we get
the list of department names and their IDs(GetDepartmentNames method). When a client selects a
department from the list, then the detailed information about department and its employees is
returned from the second server method (GetDepartmentEmployees)
In c++.
const System:: String eMployees ="Employees";
const System:: String dEpartment ="Department"i
public:
// User declarations
TUSONObject* GetDepartmentEmployees(System:: Unicodestring
AIDi
/ Get a Department and all Employees in the department
Return TUSoNObject
TUSONObjec七
TServerMethods 1:: GetDepartmentEmployees(System: Unicodestring
AID)
FDQueryDepartmentEmployees->Active = false
FDQuery Department->Active= false
FDQuery Department->Params->operator [](0)->Value = AID
FDQueryDepartmentEmployees->Params->operator [](0)->Value
AIDA
/ Create dataset list
TFDSONDatasets xds new TFDUSONDa(;
/ Add departments dataset
TEDUSONDataSetsWriter:: ListAdd(ds, dEpartment
FDQuery Department)i
/ Add employees dataset
TFDUSONDatasetsWriter:: ListAdd(ds, eMployees,
FDQuery DepartmentEmployees)i
TUSONObject *obj= new TUSonObject(i
TFDJSONInterceptor:: DataSetsToUSONObject(ds, obi)i
return obj
Apply ChangesDepartmentEmployees server method
Use this method to send data updates from client and updating the underlying database. This method
does not return any value. This method uses a TFDJSONDeltas parameter. This type is also included
in the Data. FireDACJSONReflect unit In just one operation we can update multiple tables. Here is
the source code
In Delphi:
[ SMETHODINFO ON)
public
i Public declarations
procedure Appl yChanges DepartmentEmployees(const
ADeltalist: TFDJSONDeltas)i
(SMETHODINFO OFFI
Use class completion by pressing CTRL-SHIFT-C to create a stub for this function in
the implementation section
Write code for the function you just added
8
// Update department and employees using deltas
procedure TServerMethods l Appl yChangesDepartmentEmployees
const ADeltalist: TFDUSONDeltas
Var
LApply: IFDUSONDeltasApplyUpdates;
begin
// Create the apply object
LApply TFDUSONDeltasApplyUpdates Create(ADeltalist)i
/ Apply the department delta
LApply App l yUpdates(dEpartment, FDQuery Department Command)i
if LApply Errors. Count =0 then
// If no errors, apply the employee delta
LApply. Appl yUpdates(eMployees
FDQuery DepartmentEmployees Command)i
if LApplyErrors. Count >0 then
// Raise an exception if any errors
raise Exception. Create(lApply Errors. Strings. Text)i
endi
■mnC+
public:
// User declarations
void Apply Changes DepartmentEmployees (TUSONObjectx
AUSoNObject)i
void
IServerMethods1: Apply Changes DepartmentEmployees(TUSoNObjectx
AUSONObject)
TFDUSONDeltas *ldeltas new TFDUSONDeltas(
TFDUSONInterceptor:: USONObjectToDatasets(ausonobject,
DEltas)i
TFDUSONErrors errs new TFDUSONErrors()i
/ Apply the department delta
TFDUSONDeltasApplyUpdates: ListAppl yUpdates(LDeltas
sDepartment, FDQueryDepartment->Command, errs)i
/ If no errors, apply the employee delta
if (errs->Count ==0)
TFDUSONDeltasApplyUpdates: ListAppl yUpdates(LDeltas
SEmployees, FDQueryDepartmentEmployees->Command, errs)i
9
/ Raise an exception if any erro
if(errs->Count >0)t
throw new Exception(errs->strings->Text)
Before creating the Client application, run the server
Choose Run Run Without Debugging from the main menu
Click the Start button you can minimize the server Form dialog that displays
This step is very important because you need a connection to the server to create the client classes
needed to retrieve the data from the server
Creating the Client Application
1. To create the client application in the same project group as the server application follow the
steps below:
In the Project Manager, right-click the Project Group
Select the Add New Project option
From the Delphi or the C++ Builder Projects item, select FireMonkey Desktop
Application, and click OK
Choose HD Desktop Application and click OK
Click the main menu item File New Other
From the Data Snap server node in the left column, select Data Snap REST Client
Module and press oK
10
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.