To enable remote invocation you have to connect the class for which you want to expose methods to a DSServerClass factory. (In this case, we've already done so in the database portion of the demo). The second requirement is to use a class that is compiled with the $Methodlnfo directive turned on, but this already takes place in the declaration of the base TDSServerModule class. This means that, in practice, all we have to do is to add a public method to the server module, and everything else will work.
How do we call this server method from the client application? There are basically two alternatives. One is to use the new SqlServerMethod component and call the server method as if it was a stored procedure. The second is to generate a proxy class in the client application and use this proxy class to make the call.
In the First3Tier2009 client demo I've implemented both approaches. For the first, I've added an SqlServerMethod149 component to the form of the client, tied it to the connection, picked a value for the ServerMethodName property in the Object Inspector (among the many available, as the standard lAppServer interface methods are listed as well), and checked the value of the Params property. This is a copy of the component settings (which actually include the result of a sample call performed when checking the parameters):
object SqlServerMethodl: TSqlServerMethod GetMetadata = False
149 Oddly enough the SqlServerMethod component is named with a mixed case "Sql", while all other dbExpress components use uppercase "SQL". Not a big deal, but I though it was worth noticing.
DataType = ftWideString Precision = 2000 Name = 'ReturnParameter' ParamType = ptResult Size = 2000
Value = 'Hello from TDSFirst3TierServerModule...' end>
SQLConnection = SQLConnectionl
ServerMethodName = 'TDSFirst3TierServerModule.GetHello' end
Notice that the native string type is mapped to a string parameter of 2,000 characters. After configuring the SqlServerMethod component, the program can call it using the input parameters (none in this case) and the output parameters (the result) as in a stored procedure or query call:
Sender: TObject); begi n
SqlServerMethodl.ExecuteMethod; ShowMessage (SqlServerMethod1.Params.Value); end;
To make it easier to write the calling code we can use the second approach I mentioned earlier, creating a local proxy class in the client application. To accomplish this, we can ask the Delphi IDE to parse the interface of the server class and create local proxy class for it, by clicking on the SQLConnection component and selecting the command Generate Datasnap client classes. In the case of this example, Delphi will generate a unit with the following class (from which I've omitted the code of the constructors and the destructor): type
TDSFirst3TierServerModuleClient = class private
FDBXConnection: TDBXConnection; FInstanceOwner: Boolean; FGetHelloCommand: TDBXCommand; publ ic constructor Create(
ADBXConnection: TDBXConnection); overload; constructor Create(
ADBXConnection: TDBXConnection; AInstanceOwner: Boolean); overload; destructor Destroy; override; function GetHello: string; end;
function TDSFirst3TierServerModuleClient.GetHello: string; begi n if FGetHelloCommand = nil then begi n
FGetHelloCommand := FDBXConnection.CreateCommand; FGetHelloCommand.CommandType :=
TDBXCommandTypes.DSServerMethod; FGetHelloCommand.Text :=
'TDSFirst3TierServerModu7e.GetHe77o'; FGetHelloCommand.Prepare; end;
FGetHelloCommand.ExecuteUpdate; Result := FGetHelloCommand.Parameters. Value.GetWideString;
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.