function TSimpleServerClass.SlowPrime(

MaxValue: Integer): Integer; var

I: Integer; begi n

// counts the prime numbers below the given value Result := 0;

for I := l to MaxValue do begi n if IsPrime (I) then Inc (Result);



I'll use the latter method to discuss some threading issues in practice. I've omitted the extra statements used to log the server operations from the code snippet above.

The server application has only one unit, which defines the main form and two server side classes. The form has the usual DataSnap server components, a DSServer and a DSTCPServerTransport, plus two DSServerClass component, one for each of the classes I want to expose. After compiling the server and starting it, I've let Delphi create a client proxy using the SQLCon-nection component of a new client application. This is the client proxy class: type

TSimpleServerClassClient = class private

FDBXConnection: TDBXConnection; FInstanceOwner: Boolean; FEchoCommand: TDBXCommand; FSlowPrimeCommand: TDBXCommand; publ ic constructor Create(

ADBXConnection: TDBXConnection); overload; constructor Create(

ADBXConnection: TDBXConnection; AInstanceOwner: Boolean); overload; destructor Destroy; override; function Echo(Text: string): string; function SlowPrime(MaxValue: Integer): Integer;


In the client program, the OnClick event of the button calls the Echo server method, after creating an instance of the proxy, if needed:

procedure TFormDsnapMethodsClient.btnEchoClick(

Sender: TObject); begi n if not Assigned (SimpleServer) then

SimpleServer := TSimpleServerClassClient.Create ( SQLConnectionl.DBXConnection); Editl.Text := SimpleServer.Echo(Editl.Text); end;

In the example, pressing this button the sample text "Marco" is transformed by the server call into "Marco...arco...co". This is a complete example of how you can create a totally custom server, with no database access involved and no use of the IAppServer interface. This is not the only method invocation technique available in Delphi, as you can use SOAP, socket-based applications, or third-party tools... but having this extra feature on top of the remote database access capability is certainly a plus.

One of the reasons I'm focusing on this example is it can help us clarifying some relevant features of DataSnap 2009. One of them is how server side objects relate to client proxies or server method invocation. This is better demonstrated by a server object that keeps track of its own state, like the following second server class of the demo project:

{$Methodlnfo ON} type

TStorageServerClass = class(TPersistent) private

FValue: Integer; publ ic procedure SetValue(const Value: Integer); function GetValue: Integer; function ToString: string; override; published property Value: Integer read GetValue write SetValue; end;

{$MethodInfo OFF}

While the getter and setter methods simply read and write the local field, the ToString function returns both the value and an object identifier based on its hash code:

function TStorageServerClass.ToString: string; begi n

Result := 'Value: ' + IntToStr (Value) + ' - Object: ' + IntToHex (GetHashCode, 4);

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

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.

Get My Free Ebook

Post a comment