#1 2018-05-28 00:37:48

Ehab
Member
Registered: 2016-09-07
Posts: 15

Service inside dll

Hi all,
I'm new-be to mORMot. My Question is it possible to have a service inside dll ?
My code in server will be something like :

var
  sc : IMyContainer;
  loder : IBluginLoader;
begin
  ......................
      aServer := TSQLRestServerDB.Create(aModel,ChangeFileExt(paramstr(0),'.db'),true);
//      aServer.ServiceDefine(TClientFactory,[IClientFactory],sicShared);

      loder := TBluginLoader.Create('MyDll.dll', 'CreateModuleObject'); //Load dll
      sc := IMyContainer(loder.GetBluginInterface());  // Run function "CreateModuleObject" and get result
      sc.RegisterFactory(aServer);   
 .......................
end;

My code in dll will be something like :

{ TMyContainer }

function TMyContainer.RegisterFactory(const aServer: TSQLRestServerDB): boolean;
begin
  aServer.ServiceDefine(TClientFactory,[IClientFactory],sicShared);
end;

initialization
  TInterfaceFactory.RegisterInterfaces([TypeInfo(IClientFactory)]);

thanks

Last edited by Ehab (2018-05-28 00:39:21)

Offline

#2 2018-05-28 08:07:57

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

Re: Service inside dll

If the dll has the very same interface definition, it should work (but not tested here).
Anyway, if you could rather not use a dll, it would be safer, since you may be sure that everything is properly in synch, especially as type definitions (RTTI).
And putting such logic within a dll sounds like a weird architecture choice to me.

Offline

#3 2018-05-28 09:13:32

Ehab
Member
Registered: 2016-09-07
Posts: 15

Re: Service inside dll

Thanks ab for your quick response. I tried it but it failed, I think due to RTTI may be.

I introduced a mapping unit to load the dll and call the function inside it

if my dll contains the following interface

IdllItnf = interface()
  procedure DllProcA();
  procedure DllProcB();
end;  

then my server unit will contains the following code

IServiceItnf = interface(IInvokable)
['{CF3A7E2C-8189-43F7-AE34-35920D743ED3}']
  procedure SrvcProcA();
  procedure SrvcProcB();
end;  

TMyService = class(TInterfacedObject, IServiceItnf)
private
  FDllIntf : IdllItnf;
public
  procedure SrvcProcA();
  procedure SrvcProcB();
end;  
...............

procedure TMyService.SrvcProcA();
begin
  FDllIntf.DllProcA();
end;

Now I will use the introduced classes and interfaces as regular

Offline

#4 2018-05-28 09:39:26

Ehab
Member
Registered: 2016-09-07
Posts: 15

Re: Service inside dll

ab wrote:

And putting such logic within a dll sounds like a weird architecture choice to me.

This is an old project divided into some modules each of them in dll and I want to reuse them with minimum effort.

Offline

#5 2018-05-28 13:50:23

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

Re: Service inside dll

You need the very same interface definition, with the very same methods and parameters, and the very same GUID (which seems to be missing in the dll).

Otherwise, the RTTI won't match, and the SOA features of the framework will be lost...

Offline

#6 2018-05-29 02:06:42

Ehab
Member
Registered: 2016-09-07
Posts: 15

Re: Service inside dll

Thanks again ab not only for your response but also for sharing this great project.

My conclusion is to use any dll as service in mORMot is to make a unit contains interfaces that map interfaces inside dll. So no deal with RTTI inside dll.
Even if dll dose not have interfaces and only export functions I still have to make interfaces to map functions inside dll. So I can use the dll as it is.

Offline

#7 2018-05-29 08:46:21

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

Re: Service inside dll

Indeed.

Offline

Board footer

Powered by FluxBB