您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > OLEVARIANT的替代
OLEVARIANT的替代——FIREDAC的TFDJSONDataSets和TFDJSONDeltasOLEVARIANT——这个COM的序列格式,也是DATASNAP已使用了20年的序列格式,在20年以后的今天,终于有了它的替代者:FIREDAC的TFDJSONDataSets和TFDJSONDeltas,XE5UPDATE2以上版本的DATASNAP的远程方法定义里面已经增加了这2种类型的支持。FIREDAC的TFDCONNECTION尚没有“GENERATEDATASNAPCLIENTCLASSES”的项,感觉不方便,笔者测试的时候使用TSQLCONNECTION生成的客户端代理类,易博龙稍后是否会提供?不得而知。TFDJSONDataSets:数据集对象列表,可包含N个数据集对象,不论是单表查询或多表查询(主从表)的数据都用这种类型返回给客户端。TFDJSONDeltas:客户端数据集的DELTA列表,可包含N个DELTA,不论是单表提交或多表提交(主从表)的数据都用这种类型提交给服务端。为什么要有替代品?DATASNAP为了完全去除对COM的依存,为了能跨平台。替代者的性能是否强于OLEVARIANT?笔者未作测试对比,不敢断言。下面分别给出服务端和客户端的演示代码。一)服务器端代码演示1)常量定义:constsDepartment='Department';sEmployees='Employees';2)查询数据:functionTServerMethods1.GetDepartmentEmployees(constAID:string):TFDJSONDataSets;begin//Clearactivesothatquerywillreexecute.FDQueryDepartmentEmployees.Active:=False;FDQueryDepartment.Active:=False;FDQueryDepartment.Params[0].Value:=AID;FDQueryDepartmentEmployees.Params[0].Value:=AID;//CreatedatasetlistResult:=TFDJSONDataSets.Create;//AdddepartmentsdatasetTFDJSONDataSetsWriter.ListAdd(Result,sDepartment,FDQueryDepartment);//AddemployeesdatasetTFDJSONDataSetsWriter.ListAdd(Result,sEmployees,FDQueryDepartmentEmployees);end;TFDJSONDataSets,FIREDAC的数据集列表对象,可返回多个数据集。3)提交数据:procedureTServerMethods1.ApplyChangesDepartmentEmployees(constADeltaList:TFDJSONDeltas);varLApply:IFDJSONDeltasApplyUpdates;begin//CreatetheapplyobjectLApply:=TFDJSONDeltasApplyUpdates.Create(ADeltaList);//ApplythedepartmentdeltaLApply.ApplyUpdates(sDepartment,FDQueryDepartment.Command);ifLApply.Errors.Count=0then//Ifnoerrors,applytheemployeedeltaLApply.ApplyUpdates(sEmployees,FDQueryDepartmentEmployees.Command);ifLApply.Errors.Count0then//Raiseanexceptionifanyerrors.raiseException.Create(LApply.Errors.Strings.Text);end;二)客户端演示代码1)查询数据:procedureTForm2.GetDepartmentEmployees(constADEPTNO:string);varLDataSetList:TFDJSONDataSets;LDataSet:TFDDataSet;beginLDataSetList:=ClientModule1.ServerMethods1Client.GetDepartmentEmployees(ADEPTNO);//GetdepartmentdatasetLDataSet:=TFDJSONDataSetsReader.GetListValueByName(LDataSetList,sDepartment);//UpdateUIFDMemTableDepartment.Active:=False;FDMemTableDepartment.AppendData(LDataSet);//GetemployeesdatasetLDataSet:=TFDJSONDataSetsReader.GetListValueByName(LDataSetList,sEmployees);//UpdateUIFDMemTableEmployee.Active:=False;FDMemTableEmployee.AppendData(LDataSet);end;2)提交数据:functionTForm2.GetDeltas:TFDJSONDeltas;begin//PostifeditingifFDMemTableDepartment.StateindsEditModesthenbeginFDMemTableDepartment.Post;end;ifFDMemTableEmployee.StateindsEditModesthenbeginFDMemTableEmployee.Post;end;//CreateadeltalistResult:=TFDJSONDeltas.Create;//AdddeltasTFDJSONDeltasWriter.ListAdd(Result,sEmployees,FDMemTableEmployee);TFDJSONDeltasWriter.ListAdd(Result,sDepartment,FDMemTableDepartment);end;procedureTForm2.ApplyUpdates;varLDeltaList:TFDJSONDeltas;beginLDeltaList:=GetDeltas;//Callservermethod.Passthedeltalist.ClientModule1.ServerMethods1Client.ApplyChangesDepartmentEmployees(LDeltaList);end;注意:服务端和客户端都要放置2个控件:TFDStanStorageJSONLinkTFDStanStorageBinLink
本文标题:OLEVARIANT的替代
链接地址:https://www.777doc.com/doc-2847398 .html