服务器端
function TServerMethods1.GetPerson: TPerson; begin Result := TPerson.Create; Result.FirstName := 'zyz'; Result.LastName := 'Jacky'; Result.Age := 21; end;客户端,让SQLConnection自动产生代理代码,可以的到
function TServerMethods1Client.GetPerson: TPerson; begin if FGetPersonCommand = nil then begin FGetPersonCommand := FDBXConnection.CreateCommand; FGetPersonCommand.CommandType := TDBXCommandTypes.DSServerMethod; FGetPersonCommand.Text := 'TServerMethods1.GetPerson'; FGetPersonCommand.Prepare; end; FGetPersonCommand.ExecuteUpdate; if not FGetPersonCommand.Parameters[0].Value.IsNull then begin FUnMarshal := TDBXClientCommand(FGetPersonCommand.Parameters[0].ConnectionHandler).GetJSONUnMarshaler; try Result := TPerson(FUnMarshal.UnMarshal(FGetPersonCommand.Parameters[0].Value.GetJSONValue(True))); if FInstanceOwner then FGetPersonCommand.FreeOnExecute(Result); finally FreeAndNil(FUnMarshal) end end else Result := nil; end;也就是说,客户端也会自动给unmarshal的。
调用代码
procedure TForm1.btn3Click(Sender: TObject); var p: TPerson; begin p := FServerMethod.GetPerson; with p do ShowMessage(Format('FirstName=%s, LastName=%s, Age=%d', [FirstName, LastName, Age])); //p.Free; end;由于我的FInstanceOwner使用的默认为true,UnMarshal的CreateObject产生的类,让Datasnap自己去释放了,所以p.free要注视掉。释放的时机有两个:
1。下一次调用到来
2。DBXCommand.Close
没有评论:
发表评论