You are not logged in.
Pages: 1
Hi,
I almost forgot about my new small feature already implemented some time ago : TObjProxyVariant
https://github.com/synopse/mORMot/pull/79
TObjProxyVariant is a custom variant type used to have direct access to object published properties. Very usable for cases where conversion from object to variant is not sufficient. TObjProxyVariant provides direct access to object properties from Variant instead of conversion to Variant. Example usage with mustache:
{$MODE DELPHI}
{$APPTYPE CONSOLE}
uses
mORMot, SynMustache;
type
TA = class
private
fi: integer;
function GetR: Integer;
published
property r: Integer read GetR;
property i: integer read fi write fi;
end;
TB = class
protected
fa: TA;
public
constructor Create;
destructor Destroy; override;
published
property a: TA read fa;
end;
function TA.GetR: Integer;
begin
Result := Random(100);
end;
constructor TB.Create;
begin
fa := TA.Create;
end;
destructor TB.Destroy;
begin
fa.Free;
end;
var
b: TB;
v: variant;
begin
b := TB.Create;
TObjProxyVariant.New(v, b);
v.a.i := 10;
WriteLn(TSynMustache.Parse('{{a.r}}/{{a.i}}/{{a.r}}/{{a.i}}/{{a.r}}').Render(v));
b.Free;
readln;
end.
example output :
54/10/59/10/71
best regards,
Maciej Izak
Offline
Interesting and very useful, great works !!!
Esteban
Offline
Very handy!
Usually I use duckduckdelphi in such case, but it uses Extended RTTI so in some cases it's slow.
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
thanks TObjProxyVariant can be optimized a lot : by caching TSQLPropInfoRTTI and by TSynDictionary for fast lookup for property names. Anyway current implementation is fast enough for me.
best regards,
Maciej Izak
Offline
TObjProxyVariant is here because ObjectToVariant not work for all cases. It is not art for the art but comes from practice and is widely used in my projects. Sometimes "pre generated" Variant form object is not best choice.
best regards,
Maciej Izak
Offline
Offline
Speaking of cache, does the mORMot framework has a generic object pool yet?
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
Since the beginning, there is JSON cache at TSQLRest level, which is very efficient.
Maintaining a cache of JSON is in practice a better approach that maintaining a cache of objects instances, since you can reuse the same JSON to fill several object instances, it is easier to make it threadsafe and it has lower memory fragmentation.
Instantiating an object is not worth caching: only getting the properties values may need cache.
Offline
Anyway, I've implemented it as TObjectVariant, since the idea is great.
See https://synopse.info/fossil/info/d5d49cdd32
It should be faster, supports TSQLRecord.ID as expected, and JSON serialization.
Offline
Thanks, for improved/faster implementation. I think that TObjectVariant is better name . The description "lazy-loading to object properties from Variant" is perfect description of my intention.
best regards,
Maciej Izak
Offline
Pages: 1