DSProxyGenerator (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demostrates generation of a DataSnap client proxy classes unit while excluding specific methods programatically using a DBX connection.

Create this proxy generator project by creating a Delphi console application: File > New > Other > Delphi Projects > Console Application. Copy the code below into the module created. Add a DataSnap server project to the group project by right clicking on ProjectGroup1 at the top of the Project Manager frame and selecting Add New Project. Then select Delphi Projects > DataSnap Server > DataSnap Server to start the DataSnap Server Wizard. Use the default settings for the wizard:

  • VCL Forms Appication or Console Application
  • Protocols TCL/IP, Server Method Class
  • TCP/IP Port: 211
  • TComponent

To run:

  1. Right-click on Project2.exe (DSServer) in the Project Manager window.
  2. Select Run Without Debugging. A small TForm should come up to indicate that the server is running.
  3. Right-click on Project1.exe (DSProxyGenerator) and select Run.
  4. A console window should come up briefly and C:\GeneratedProxy.pas should be created.

Code

uses
  SysUtils, DSClientMetadata, SqlExpr, DSProxyDelphi, Datasnap.DSProxyDelphiNative, Data.DbxDatasnap, IPPeerClient, Datasnap.DSConnectionMetaDataProvider;

var
  SqlConnection: TSqlConnection;
  DSConnectionMetaDataProvider: TDSConnectionMetaDataProvider;
  ProxyGen: TDSProxyGenerator;
const
  NL = #13#10;
  ConnectionStr = 'DriverName=DataSnap'+NL+'Port=211'+NL;
begin
  SqlConnection := TSqlConnection.Create(nil);
  try
    SqlConnection.DriverName := 'DataSnap';
    SqlConnection.Params.Text := ConnectionStr;
    DSConnectionMetaDataProvider := TDSConnectionMetaDataProvider.Create(SqlConnection);
    DSConnectionMetaDataProvider.SQLConnection := SqlConnection;
    ProxyGen := TDSProxyGenerator.Create(SqlConnection);
    ProxyGen.MetaDataProvider := DSConnectionMetaDataProvider;
    ProxyGen.ExcludeMethods := 'EchoString;ReverseString';
    ProxyGen.Writer := sDelphiDBXProxyWriter;
    ProxyGen.TargetDirectory := 'C:\';
    ProxyGen.TargetUnitName := 'GeneratedProxy.pas';
    ProxyGen.Write;
  finally
    SqlConnection.Free;
  end;
end.

Uses

Code Examples

See Also