#1 2021-09-16 18:51:33

BenTen
Member
Registered: 2021-01-19
Posts: 11

How to store IInterface variable in ORM

Hi, ab
Help me please
How correctly to convert IInterface type to store in ORM database?

unit Unit1;

interface
type
  ISomeInterface = interface
    ['{0A799B7B-3BBB-4159-B59F-BD3B03AA654F}']
    function GetProvider: RawUTF8;
  end;

  TSQLProviders = class(TSQLRecord)
  private
    FIntf: ISomeInterface;                                                 {What type of interface variable?}
  published
    property Intf: ISomeInterface read FIntf write FIntf;
  end;

  TSomeObject = class
  protected
    FIntf: ISomeInterface;
    FRest: TSQLRest;
  public
    constructor Create;
  end;

implementation

constructor TSomeObject.Create;
var
  Rec: TSQLProviders;
  Provider: RawUTF8;
begin
   FRest := TSQLRestServerFullMemory.CreateWithOwnModel([TSQLProviders]);
   FRest.CreateMissingTables;
   TSQLProviders.AutoFree(Rec);

   Intf := TSomeProvider.Create;
   Rec.Intf :=  Intf;                               {how to convert interface variable to store in database?}
   FRest.Add(Rec,'',True);

   Provider := Rec.Intf.GetProvider;        {how to call the interface method from database?}

end;


end.

Offline

#2 2021-09-16 18:55:36

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

Re: How to store IInterface variable in ORM

It is not supported, nor possible.

How could such an interface be unserialized?
You are messing things up.

Offline

#3 2021-09-16 20:27:00

BenTen
Member
Registered: 2021-01-19
Posts: 11

Re: How to store IInterface variable in ORM

Did the IInterface type variable is not a pointer?

Offline

#4 2021-09-16 21:08:32

BenTen
Member
Registered: 2021-01-19
Posts: 11

Re: How to store IInterface variable in ORM

Thanks
That's how it works

...
 TSQLProviders = class(TSQLRecord)
  private
    FIntf: UIntPtr;                                                 {What type of interface variable?}
  published
    property Intf: UIntPtr read FIntf write FIntf;
  end;
....
    Intf := TSomeProvider.Create;
   Rec.Intf :=  UIntPtr(Intf);                                            {how to convert interface variable to store in database?}
   FRest.Add(Rec,True);
....
   Provider := ISomeInterface(Rec.Intf).GetProvider;        {how to call the interface method from database?}

Offline

#5 2021-09-17 07:18:55

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

Re: How to store IInterface variable in ORM

You can't persist an interface pointer.
It is depending on the execution context.
If you restart the application for instance, the pointer value will be incorrect for sure.
So I don't see the point of saving a pointer into a database.

We can save classes, because we don't save their pointer, but their attributes/fields values.

What you could do is save an integer ID which may be able to create an interface instance from the database.
But this is something else.

Offline

#6 2021-09-17 15:00:12

BenTen
Member
Registered: 2021-01-19
Posts: 11

Re: How to store IInterface variable in ORM

Thanks, ab

ab wrote:

You can't persist an interface pointer.
It is depending on the execution context.

This is just what I need.
I need to save an interface pointer within the execution context into the FullInMemory database

Offline

#7 2021-09-18 13:00:08

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

Re: How to store IInterface variable in ORM

You could use the PtrUInt() type cast but be aware that it won't increase the reference count: it returns a "weak" interface instance.

Offline

#8 2021-09-21 20:23:16

BenTen
Member
Registered: 2021-01-19
Posts: 11

Re: How to store IInterface variable in ORM

Thanks, ab.
I understood it.
I have to use InterfaceArrayAdd and InterfaceArrayDelete each time I add or delete the interface into the database

Offline

Board footer

Powered by FluxBB