2012-05-16 56 views
1

如何在datasnap服務器中處理對象引用?Datasnap和TObject引用

我有返回TUSER對象

TServerMethods.CurrentUser(const AId: Integer): TUser 
begin 
    Result := GlobalCurrentUser; // <- Don't want to free this object 
    Result.AddReference; 
end; 

TUSER在子類TReferencedObject

TReferencedObject = class(TObject) 
private 
    FReferenceCnt:  integer; 
public 
    procedure AddReference; virtual; 
    procedure RemoveReference; virtual; // Frees object if reference count goes to 0 
    property ReferenceCnt: integer Read FReferenceCnt Write FReferenceCnt; 
end; 

的DataSnap服務器方法目前的DataSnap服務器釋放TUSER對象,但我想打電話給RemoveReference爲TReferencedObject後裔。

回答

1

我複製的DataSnap文件Data.DBXJSONReflect.pas和Datasnap.DSReflect.pas項目目錄,添加到項目,並進行了三次修改。在Data.DBXJSONReflect

兩個變化來設定正確ReferenceCount

1)TJSONUnMarshal.CreateObject

if HasObject(ObjId) then 
    begin 
    Obj := GetObject(ObjId); 
    if Obj is TReferencedObject then {Framework} 
     TReferencedObject(Obj).AddReference; {Framework}  
    exit(Obj); 
    end 

2)TJSONPopulationCustomiser.PostPopulate

if Data is TReferencedObject then {Framework} 
    TReferencedObject(Data).AddReference; {Framework} 

一個變化中Datasnap.DSReflect釋放參考

1)TDsMethodVa清除參考參數

changed FUserObject.Clear to ClearUserObjects(FUserObjects);並添加本地函數

procedure ClearUserObjects(const AUserObjects: TObjectList<TObject>); {Framework} 
    var 
    i: Integer; 
    begin 
    FUserObjects.OwnsObjects := False; 
    for i := FUserObjects.Count - 1 downto 0 do 
     if FUserObjects.Items[i] is TReferencedObject then 
     begin 
     TReferencedObject(FUserObjects.Items[i]).RemoveReference; 
     FUserObjects.Delete(i); 
     end; 
    FUserObjects.OwnsObjects := True; 
    FUserObjects.Clear; 
    end;