#1 2019-01-28 15:02:26

lagodny
Member
Registered: 2015-01-28
Posts: 50

Nested classes in Cross-Platform

Are there any plans to add support for nested classes in the Cross-Platform?

Offline

#2 2019-01-29 08:10:48

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

Re: Nested classes in Cross-Platform

What do you call "nested classes"?
Joined ORM fields?

The idea with cross-platform clients is that you rather define DTO objects, not directly access complex ORM classes.

There is no plan to support it yet.
But any input is welcome!

Online

#3 2019-01-30 08:00:31

lagodny
Member
Registered: 2015-01-28
Posts: 50

Re: Nested classes in Cross-Platform

I meant something like this:

  TMyParams = class
  private
    FP1: Boolean;
  published
    property P1: Boolean read FP1 write FP1;
  end;

  TMyRec = class(TSQLRecord)
  private
    FTitle: string;
    FParams: TMyParams;
  public
    destructor Destroy; override; // to destroy FParams
    class function NewInstance: TObject {$IFDEF AUTOREFCOUNT} unsafe {$ENDIF}; override; // need override NewInstance to create FParams (constructor TObjects isn't virtual)
  published
    property Title: string read FTitle write FTitle;
    property Params: TMyParams read FParams write FParams;
  end;

const
  cStoreClassName = True;
...
  r := TMyRec.Create;
  try
    r.Title := 'Hello';
    r.Params.P1 := True;
    aJSON := ObjectToJSON(r, cStoreClassName);
  finally
    r.Free;
  end;

  r := TMyRec(JSONToNewObject(aJSON))

I made some tests.
1. The function ObjectToJSON have parameter StoreClassName: boolean=false. So we don't have ClassName to create nested classes in SynCrossPlatformREST by default. Is it possible to make it customizable?
2. The code works on Win32. On Android there is a problem with
function JSONToNewObject(const JSON: string):  pointer;
should be
function JSONToNewObject(const JSON: string): TObject;

And a question. Is it possible to somehow teach the mORMotWrappers to generate nested classes? Or should I put them in the template? Or I should use nested records and arrays?

Offline

#4 2019-01-30 17:40:54

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

Re: Nested classes in Cross-Platform

Nested records and arrays, defined as DTO is the right way to do it.

For your Params field, I think you could just use a variant for TMyRec definition, which will create a JSON variant.

Online

#5 2019-01-30 19:42:00

lagodny
Member
Registered: 2015-01-28
Posts: 50

Re: Nested classes in Cross-Platform

Thanks, ab

Offline

Board footer

Powered by FluxBB