您的位置:首页 > 编程语言 > Delphi

Delphi2009(Tiburon)强化 DataSnap 技术

2009-03-24 14:26 465 查看
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:





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:





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:





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:





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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: