Delphi2009(Tiburon)强化 DataSnap 技术

本文介绍了一种新的基于组件的DataSnap服务器技术,该技术不再支持DCOM,而是使用JSON/RPC消息传递层和可插拔传输层。文章详细介绍了如何创建服务器方法、使用服务器连接以及托管现有的远程数据模块。
摘要由CSDN通过智能技术生成

Delphi now provides a new component based DataSnap server technology. Existing applications can host their RemoteDataModules inside this new DataSnap server. Our new DataSnap server does not support the DCOM technology. If you still need DCOM, the older component set for DataSnap is still included in the product. For the new DataSnap server, JSON/RPC based messaging layer and pluggable transport layer is provided.

In addition to supporting existing RemoteDataModules, we now provide support for server methods and server connections. Server methods are our light weight mechanism for remote method invocation. They are ideal for writing stored procedure logic in an object oriented language such as Object Pascal. Server methods can be invoked from dbExpress or ADO.NET clients in the same way that stored procedures are. This is a very simple, easy to use mechanism for remote method invocation. The DataSnap server uses dynamic method invocation to execute server methods. This approach allows simple methods on plain old objects to be called from remote clients. There is no need for a proprietary interface definition language or complicated tools to generate stubs and proxies.

Server connections allow a client and server application to share a server side dbExpress database connection. Only the DBXClient driver is needed on the client to access any dbExpress driver installed on the server. The DBXClient is 100% Object Pascal making for a simple client side deployment. Server connections allow a client to share the same database connection and transactional context with a server application. An interesting application of server connections is to allow the client to perform select queries to read data, and to call server methods that use the same server connection to perform all insert, update and delete operations on the data.

The new DataSnap components are implemented in Object Pascal. We currently provide win32 server side applications. On the client we support both win32 and .net applications. Server methods and server connections can both be accessed through both our dbExpress and ADO.NET connectivity solutions. Going forward, we’d also like to support more client and server side platforms including javascript, php, .net and java.

Creating Server methods.


Delphi developers will most likely want to add their server methods to a TDataModule class. Delphi classes that contain server methods must be compiled with METHOD_INFO enabled because we use
Delphi’s RTTI for the dynamic invocation of methods.. All server methods must be made public. Here is a simple example of a TDataModule with server methods:

i1_699.jpg

Server methods parameters directions can be specified as input, input/output (var), output (out), and return (assign to Result). Parameter types supported include the scalar values supported by dbExpress/ADO.NET drivers such as integer, string, date, time, floating point, etc. Very large binary parameters can be passed as a TStream. A TParams object can be used to pass an array of values or to return a single row result to the client.Table shaped objects such as a TDataSet or a TDBXReader can be used as parameters and return values. On the .net platform, a DataReader, DataView or DataTable can be used as parameters by the client. Note that for table parameters the object type used on the client does not have to match the object type used in the signature of the server method. This is because the messaging layer maps all table objects to a TDBXReader.Custom TDBXReaders are fairly simple to implement. It should not be too difficult to map a collection of object for an OR mapping technology into a TDBXReader inside a server method and return it to a client which reads it into a TDataSet.The implementation of these simple server methods is listed below:

i2_701.jpg

Exposing server methods to clients

Once you have implemented your server methods, they must be exposed by a DataSnap server. This is easy. Its best to create a separate DataModule for your DataSnap server. Create a new DataModule and drop three components from the new “DataSnap Server” component category: TDSServer, TDSTCPServerTransport, and TDSServerClass. Set the TDSTCPServerTransport.Server property and the TDSServerClass.Server properties to the TDSServer. The TDSServerClass.OnGetClass event must be set to your server class like this:

i3_703.jpg

Now execute the application and the server will be started automatically. There is a TDSServer.AutoStart property that defaults to true which causes the server to start when it is notified that the component is being loaded.

Calling server methods from a client

From the client’s perspective, server methods look a lot like stored procedures. There are some notable exceptions though. Server methods can send multiple table objects from the server to the client and from the client to the server. Here is a simple example of a client calling one of the server methods above from within a dunit test case:

i4_705.jpg

Using server connections

The DataSnap dbExpress connection can be used by a client application to access a server side dbExpress connection. By default, a DataSnap dbExpress connection is only able to execute server methods on the DataSnap server it is connected to. If the TDBXPropertyNames.ServerConnection property is set to a server method that returns an instance of TDBXConnection, then the client side DataSnap connection will also be able to execute sql commands against the returned TDBXConnection.   Here is an example of a server method that returns an instance of TDXConnection:

A client side connection can establish associate itself with this connection by setting its ServerConnection connection property to:

ServerConnection=ServerMethodsDataModule.GetServerConnection

Note that there is a built-in Server method called DSAdmin.GetConnection that can be used to establish server connections using the dbxconnections.ini on the server. This server method has a string input parameter which is used to specify what connection to use. This example shows how to set the server connection to an ibconnection item in the dbxconnections.ini file:

ServerConnection=DSAdmin.GetConnection("ibconnection")

Hosting existing remote data modules

RemoteDataModules are configured similar to any other server class. As with other server classes, you will need to use a TDSServerClass component and wire the OnGetClass event to return your RemoteDataModule class. On the client, you will need to use the new TDBXAppServerConnection.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值