#1 2015-10-22 17:17:17

dexter
Member
Registered: 2015-04-24
Posts: 53

Get instance of service

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

#2 2015-10-22 18:45:15

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

Offline

#3 2015-10-23 07:31:20

dexter
Member
Registered: 2015-04-24
Posts: 53

Re: Get instance of service

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

#4 2015-10-23 07:43:46

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

Re: Get instance of service

I do not see any dependency of TInterfaceResolverInjected...
It is just a class storing lists of instances.

Offline

#5 2015-10-23 08:42:27

dexter
Member
Registered: 2015-04-24
Posts: 53

Re: Get instance of service

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

#6 2022-07-08 12:42:54

xalo
Member
Registered: 2016-09-22
Posts: 32

Re: Get instance of service

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

Board footer

Powered by FluxBB