TROSession has a procedure to save a complexType to a session. I was wondering why the opposite was not implemented. Session.LoadStruct.
Some safe assumptions like if the field name is not found on the session it will not be filled up with data, only basic objects in the same way savestruct works will be acceptable and will be an extremely useful simple function to have built in.
SaveStruct method was added in 2003. it not used nowadays in ROD.
from 1st sight, it isn’t optimal implementation because you can lose manually added values.
also if you save two objects that have similar properties, it will override already added values.
If you are using InMemorySessionManager, you can store structs as is, like
function ObjectToVariant(aObject: TObject): Variant;
begin
Result := Null;
if aObject <> nil then begin
TVarData(Result).VType := varByRef;
TVarData(Result).VPointer := aObject;
end;
end;
function VariantToObject(aVariant: Variant): TObject;
begin
result := nil;
if TVarData(aVariant).VType = varByRef then
result := TObject(TVarData(aVariant).VPointer);
end;
type
TMyComplex = class(TROComplexType)
private
fValue: String;
published
property Value: String read fValue write fValue;
end;
procedure TForm87.Button1Click(Sender: TObject);
var
s: TROSession;
i: Integer;
m: TMyComplex;
begin
s := TROSession.Create(NewGuid);
try
for i := 0 to 5 do begin
m := TMyComplex.Create;
m.Value := i.ToString;
s.Values['item'+i.ToString]:= ObjectToVariant(m);
end;
for i := 0 to 5 do
Memo1.Lines.Add(TMyComplex(VariantToObject(s.Values['item'+i.ToString])).Value);
finally
s.Free;
end;
end;