You are not logged in.
Pages: 1
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
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
Can you show sample code how to implement it using TInterfacedObjectRest ?
Offline
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
No way, my customer does not have information about database connection
Offline
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
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
No way, my customer does not have information about database connection
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
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
Pages: 1