Last active
June 24, 2020 09:30
Test sample for mORMot lazy DI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit MyTestClientMain; | |
interface | |
uses | |
{$IFDEF MSWINDOWS} Windows, Messages, {$ENDIF} | |
SysUtils, Variants, Classes, Graphics, Controls, Forms, | |
Dialogs, StdCtrls, | |
SynCommons, mORMot, mORMotHttpClient, MyTestInterface; | |
type | |
TForm1 = class(TForm) | |
edtA: TEdit; | |
edtB: TEdit; | |
lblA: TLabel; | |
lblB: TLabel; | |
btnCall: TButton; | |
btnCancel: TButton; | |
lblResult: TLabel; | |
procedure btnCancelClick(Sender: TObject); | |
procedure btnCallClick(Sender: TObject); | |
procedure FormDestroy(Sender: TObject); | |
private | |
{ Private declarations } | |
public | |
{ Public declarations } | |
Model: TSQLModel; | |
Client: TSQLRestClientURI; | |
end; | |
var | |
Form1: TForm1; | |
implementation | |
{$R *.dfm} | |
{$ifndef FPC} | |
{$R Vista.res} | |
{$endif} | |
procedure TForm1.btnCancelClick(Sender: TObject); | |
begin | |
Close; | |
end; | |
procedure TForm1.btnCallClick(Sender: TObject); | |
var a,b: integer; | |
err: integer; | |
I: IMyTestService; | |
begin | |
val(edtA.Text,a,err); | |
if err<>0 then begin | |
edtA.SetFocus; | |
exit; | |
end; | |
val(edtB.Text,b,err); | |
if err<>0 then begin | |
edtB.SetFocus; | |
exit; | |
end; | |
if Client=nil then begin | |
if Model=nil then | |
Model := TSQLModel.Create([],ROOT_NAME); | |
Client := TSQLRestClientURINamedPipe.Create(Model,APPLICATION_NAME); | |
if not Client.ServerTimeStampSynchronize then begin | |
ShowMessage(UTF8ToString(Client.LastErrorMessage)); | |
exit; | |
end; | |
if not Client.SetUser('User', 'synopse') then | |
begin | |
ShowMessage(UTF8ToString(Client.LastErrorMessage)); | |
FormDestroy(self); | |
exit; | |
end; | |
Client.ServiceDefine([IMyTestService],sicShared); | |
end; | |
if Client.Services['MyTestService'].Get(I) then | |
lblResult.Caption := IntToStr(I.DoTheJob(a,b)); | |
end; | |
procedure TForm1.FormDestroy(Sender: TObject); | |
begin | |
FreeAndNil(Client); | |
FreeAndNil(Model); | |
end; | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit MyTestInterface; | |
interface | |
type | |
IMyTestService = interface(IInvokable) | |
['{9B031536-EBF1-4F41-BFB8-54AB804D85D9}'] | |
function DoTheJob(n1,n2: integer): integer; | |
end; | |
ICalculator = interface(IInvokable) | |
['{9A60C8ED-CEB2-4E09-87D4-4A16F496E5FE}'] | |
function Add(n1,n2: integer): integer; | |
end; | |
const | |
ROOT_NAME = 'root'; | |
PORT_NAME = '8888'; | |
APPLICATION_NAME = 'RestService'; | |
implementation | |
uses | |
mORMot; | |
initialization | |
// so that we could use directly ICalculator instead of TypeInfo(ICalculator) | |
TInterfaceFactory.RegisterInterfaces([TypeInfo(IMyTestService), | |
TypeInfo(ICalculator)]); | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// this server will use TSQLRestServerFullMemory kind of in-memory server | |
// - it will only by 200 KB big with LVCL :) | |
program MyTestServerInMemory; | |
{$APPTYPE CONSOLE} | |
uses | |
{$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads | |
SysUtils, | |
Classes, | |
SynCommons, | |
mORMot, | |
MyTestInterface; | |
type | |
TServiceCalculator = class(TInterfacedObject, ICalculator) | |
public | |
function Add(n1,n2: integer): integer; | |
end; | |
TMyTestService = class(TInjectableObjectRest, IMyTestService) | |
private | |
fCalculator: ICalculator; | |
public | |
function DoTheJob(n1,n2: integer): integer; | |
published | |
property Calculator: ICalculator read fCalculator; | |
end; | |
var | |
aModel: TSQLModel; | |
{ TMyTestService } | |
function TServiceCalculator.Add(n1, n2: integer): integer; | |
begin | |
result := n1+n2; | |
end; | |
function TMyTestService.DoTheJob(n1, n2: integer): integer; | |
begin | |
if assigned(fCalculator) then | |
Result := fCalculator.Add(n1,n2) | |
end; | |
begin | |
aModel := TSQLModel.Create([],ROOT_NAME); | |
try | |
with TSQLRestServerFullMemory.Create(aModel,'test.json',false,true) do | |
try | |
ServiceDefine(TServiceCalculator,[ICalculator],sicShared); | |
ServiceDefine(TMyTestService,[IMyTestService],sicShared); | |
if ExportServerNamedPipe(APPLICATION_NAME) then | |
writeln('Background server is running.'#10) else | |
writeln('Error launching the server'#10); | |
write('Press [Enter] to close the server.'); | |
readln; | |
finally | |
Free; | |
end; | |
finally | |
aModel.Free; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment