#1 2020-11-04 14:00:42

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

SOA - inject extra data into created object

1.

type
  IQueues = interface(IInvokable)
    ['{9857C187-68A2-4F22-823C-75B75F28B45A}']
    function GetQueues(out response: TQueuesCollection): TResponse;
  end;

2.

type
  TQueues = class(TInterfacedObject, IQueues)
  private
    connectionString: string;
  public
    constructor Create; override;
    function GetQueues(out response: TQueuesCollection): TResponse;  -> here will be "select * from queues"
  end;

How to pass for example ConnectionString to the created object, something like:

with Server.ServiceDefine(TQueues,[IQueues]) do
  begin
     onCreate:= myOnCreate;
  end;

Offline

#2 2020-11-04 17:19:33

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

Re: SOA - inject extra data into created object

You can't directly.

What you may do is either
- use a global variable,
- or use TInterfacedObjectRest and transtype the TSQLRest instance to your own private class with the proper connection string as property
- or use an internal interface to resolve

Offline

#3 2020-11-04 20:36:32

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: SOA - inject extra data into created object

Can you show sample code how to implement it using TInterfacedObjectRest ?

Offline

#4 2020-11-05 03:48:46

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: SOA - inject extra data into created object

I guess you should/can pass it using a parameter of the GetQueues method?


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#5 2020-11-05 07:50:46

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: SOA - inject extra data into created object

No way, my customer does not have information about database connection lol

Offline

#6 2020-11-05 10:07:34

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

Re: SOA - inject extra data into created object

You can also assign an event to TSQLRestServer.OnServiceCreateInstance.

    /// this event will be executed by TServiceFactoryServer.CreateInstance
    // - you may set a callback to customize a server-side service instance,
    // i.e. inject class-level dependencies:
    // !procedure TMyClass.OnCreateInstance(
    // !  Sender: TServiceFactoryServer; Instance: TInterfacedObject);
    // !begin
    // !  if Sender.ImplementationClass=TLegacyStockQuery then
    // !    TLegacyStockQuery(Instance).fDbConnection := fDbConnection;
    // !end;
    // - consider using a TInjectableObjectClass implementation for pure IoC/DI
    OnServiceCreateInstance: TOnServiceCreateInstance;

Offline

#7 2020-11-05 13:58:07

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: SOA - inject extra data into created object

Thank you AB, this is what I needed.

type
  IExtraInfo = interface
    ['{97B05BCF-D2E7-4BE3-B56B-BD43552F411B}']
    procedure SetConnectionString(const value: string);
  end;

  ITest = interface(IInvokable)
  ['{20CF8A1A-95AC-446A-ABEF-9379E7DFA47B}']
    procedure Test;
  end;

  TTest = class(TInterfacedObject, ITest, IExtraInfo)
  private
    connectionString: string;
    procedure Test;
    procedure SetConnectionString(const value: string);
  end;

...
procedure TForm3.Button1Click(Sender: TObject);
begin
  var Server := TSQLRestServerFullMemory.CreateWithOwnModel([], false, 'api');
  Server.CreateMissingTables;
  Server.OnServiceCreateInstance := Instance;
end;

procedure TForm3.Instance(Sender: TServiceFactoryServer;
  Instance: TInterfacedObject);
begin
  var intf: IExtraInfo;
  if Supports(Instance,IExtraInfo, intf) then
    intf.SetConnectionString('Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;');
end;

Offline

#8 2020-11-05 14:53:18

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: SOA - inject extra data into created object

radexpol wrote:

No way, my customer does not have information about database connection lol

What I meant is the client passes in a 'ClientId', which allows your service method to get/initialize the actual connection string.

But yes, ab had the perfect answer already wink


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

Board footer

Powered by FluxBB