#1 2017-11-25 07:32:15

Hmdsadeghian
Member
Registered: 2017-11-25
Posts: 31

Problem with define property in interface

Hi all,

This is my Interface

 
unit UntIConnectionProperties;

interface
uses
  syncommons;

Type
  TConnectionData=packed record
    fServerName :RawUTF8;
    fUserName :RawUTF8;
    fPassword :RawUTF8;
  end;

  IConnectionProperties =Interface(IInvokable)
    ['{391FC485-33AF-42C8-B4C7-1C87EF850D0D}']
    function GetConnectionData: TConnectionData;
    procedure SetConnectionData(aValue :TConnectionData);
    property ConnectionData :TConnectionData read GetConnectionData write SetConnectionData;
  End;

and this is my class

 
unit UntTConnectionProperties;

interface
uses
  UntIConnectionProperties,
  inifiles,
  System.SysUtils,SynCommons;

Type
  TConnectionProperties =Class(TInterfacedObject,IConnectionProperties)
  private
    fConnectionINI: TIniFile;
  public
    constructor create;
    destructor Destroy; override;
    property ConnectionINI :TIniFile read fConnectionINI write fConnectionINI;
    function GetConnectionData:TConnectionData;
    procedure SetConnectionData(aValue :TConnectionData);
  published
    property ConnectionData :TConnectionData read GetConnectionData write SetConnectionData;
  End;

and I register this service with this code

 
    aServer.ServiceRegister(TConnectionProperties,[Typeinfo(IConnectionProperties)],sicClientDriven).
            SetOptions([],[optExecInMainThread,optFreeInMainThread]);

at the compile time I don't have any error but when running service and service want to be register, I get error message that parameter in SetConnectionData must be const,var or out.

how can I solve this problem?

thanks

Offline

#2 2017-11-25 08:55:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Problem with define property in interface

Just do what the compiler says:

    procedure SetConnectionData(const aValue :TConnectionData);

This is due to low-level way of parameters marshalling at asm level: without a const, there is a temporary copy made.
It is not only slower, but also unneeded in the SOA context.

Anyway, a good advice is to always define records, arrays or variant parameters as const.
It will have a performance benefit, even outside of mORMot.

Offline

#3 2017-11-25 10:16:51

Hmdsadeghian
Member
Registered: 2017-11-25
Posts: 31

Re: Problem with define property in interface

Thanks.
But I want to pass value to this property from client. Also I don't know how to using constructor with parameters in client side.
at client side I use this code for using my service

  fClient.ServiceRegisterClientDriven(typeInfo(IConnection),fService);
  fClient.ServiceRegisterClientDriven(typeInfo(IConnectionProperties),fConnectionProperties);
 

But how to using constructor or pass parameters to interface property from client side that property defined const?

Another Question is when the constructor of Class service is called?
for example at below code

 
Type
  TConnection =class(TInterfacedObject,IConnection)
  protected
     fDbProps :TSQLDbConnectionProperties;
     fConnectionProp :IConnectionProperties;
  public
     Constructor create;
     function GetConnectionProp:TConnectionData;
     procedure SetConnectionProp(var aValue :TconnectionData);
     procedure Connect (Var ConnectionData :TConnectionData);
  end;

when the constructor is called? and I don't know that I really need the constructor or not?

Last edited by Hmdsadeghian (2017-11-25 11:24:16)

Offline

#4 2017-11-25 14:05:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Problem with define property in interface

On client side, there is no implementation class.
On server side, the plain constructor of the implementation class is always called with no parameter.

So what you can do is to use sicClientDriven, then define a setup method in the interface to define the expected parameters.

Also note that inheriting from TInterfacedObject will never call the overriden constructor (i.e. TConnection.Create) on the server side.
But if you inherit from TInterfacedObjectWithCustomCreate, you will have a virtual constructor, which will be called on the server side as you expect.

Offline

#5 2017-11-25 18:35:01

Hmdsadeghian
Member
Registered: 2017-11-25
Posts: 31

Re: Problem with define property in interface

Thanks a lot

Offline

Board footer

Powered by FluxBB