You are not logged in.
Pages: 1
Hi,
I have a REST Server:
Server : TSQLRestServer;
I have a class TServer which implements service IServer:
TServer = class(TInterfacedObject, IServer)
I define the service IServer:
Server.ServiceDefine(TServer, [IServer], sicShared);
I can obtain the interface which implements the service:
Server.Services.Resolve(IServer, Srv); // Srv is declared as IServer
My question is: is there a way to obtain the instance of TServer which implements the IServer interface?
Regards,
Alex.
Offline
Offline
It does not help me, since I need to access the server interface outside the service context (i.e. outside TServiceFactoryServer.ExecuteMethod).
Here is my goal: I want to reduce coupling in my project.
I have UnitServer where Server : TSQLRestServer is defined.
In other units (UnitServerA, UnitServerB, etc.) I need to access the IServer and I don't want to include UnitServer in their uses clause.
So my idea is to define a service container (TInterfaceResolverInjected) in a separate unit and add there the implementations of all services I need to acces from outside (TInterfaceResolverInjected.InjectInstance()).
After that, in all units (UnitServerA, UnitServerB, etc.) I just do TInterfaceResolverInjected.Resolve() to access the service.
The problem is that TInterfaceResolverInjected.InjectInstance() needs the instance of TServer, which is created by Server.ServiceDefine() and this instance I'm trying to obtain.
Hope it clarifies.
Regards.
Offline
The dependency is for TServer (which is class(TInterfacedObject, IServer).
That's why I want to add the TServer to TInterfaceResolverInjected and use it all over the project to access IServer, without doing uses UnitServer.
And I cannot obtain the TServer instance when doing TSQLRestServer.ServiceDefine().
Can I get it from TSQLRestServer somehow?
Offline
You can obtain i.e. when calling .ServiceDefine:
var ServiceFactoryServer : TServiceFactoryServer;
InstanceOfTServer : IServer;
...
ServiceFactoryServer := Server.ServiceDefine(TServer, [IServer], sicShared);
ServiceFactoryServer.Obj(InstanceOfTServer);
If you also want to access to a TSQLRestServer instance simply change class definition by the following
TServer = class(TInjectableObjectRest, IServer)
See Implement your service from TInjectableObjectRest
Note: IMHO your naming in your example it's a little confusing
Last edited by xalo (2022-07-08 15:14:59)
Offline
Pages: 1