#1 2022-05-10 10:24:18

tbo
Member
Registered: 2015-04-20
Posts: 353

Description class TSynAutoCreateFields

I think the description to class TSynAutoCreateFields is not consistent. You write: "...any class defined as a published property will be owned by this instance". And elsewhere you write: "...this overriden constructor will instantiate all its nested TPersistent/TSynPersistent/TSynAutoCreateFields published properties". But it also works with descendants of TObject:

type
  TSubItem = class(TObject)
  private
    FSubValue: RawUtf8;
  published
    property SubValue: RawUtf8 read FSubValue write FSubValue;
  end;

  TItem = class(TSynAutoCreateFields)
  private
    FValue: RawUtf8;
    FSubItem: TSubItem;
  published
    property Value: RawUtf8 read FValue write FValue;
    property SubItem: TSubItem read FSubItem;
  end;

var
  item: TItem;
  list: TObjectList;
begin
  list := TObjectList.Create;
  try
    item := TItem.Create;
    item.Value := StringToUtf8('value' + 1.ToString);
    item.SubItem.SubValue := StringToUtf8('subValue' + 1.ToString);
    list.Add(item);

    ObjectToJsonFile(list, '_listData.json');
  finally
    list.Free;
  end;
end;

With best regards
Thomas

Offline

#2 2022-05-10 13:23:04

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

Re: Description class TSynAutoCreateFields

You are right, it sounds confusing.
The idea was that you need to have RTTI enabled for the class instance, which is the case for TPersistent/TSynPersistent. But not for a plain TObject.

Your code TSubItem = class(TObject) will force to generate the RTTI for it, even if it inherits from plain TObject, because you defined published properties - there is in fact a warning in the compiler messages.
But there is in fact no limitation to TPersistent/TSynPersistent/TSynAutoCreateFields classes.

I have made the comments a little more explicit.
Please see https://github.com/synopse/mORMot2/commit/11283a76

Offline

Board footer

Powered by FluxBB