一窥kbmmw中的 smart service

在kbmmw 的新版中(还没有发布),将会有一个叫做smart service 的服务。这种服务的属性基于服务器端,
并且可以自动注册服务名,下面就是一个简单例子代码。这个服务里面有有三个发布的函数:echostring,
EchoReversedString和AddNumbers。这些函数使用一些声明变量,包括“魔法”参数变量,以便访问不同的
客户端标识值。


复制代码
 [kbmMW_Service('SMARTDEMO')]
  TkbmMWCustomService2 = class(TkbmMWCustomSmartService)
  private
  protected
  public
     [kbmMW_Method]
     function EchoString(const AString:string):string;

     [kbmMW_Method('EchoReversedString')]
     function ReverseString(const AString:string;
                         [kbmMW_Arg(mwatClientIdentity)]
                         const AClientIdentity:TkbmMWClientIdentity
                           ):string;

     [kbmMW_Method]
     function AddNumbers(const AValue1,AValue2:integer;
                         [kbmMW_Arg(mwatRemoteLocation)]
                         const ARemoteLocation:string
                        ):integer;
  end;

...
function TkbmMWCustomService2.EchoString(const AString:string):string;
begin
     Result:=AString;
end;

function TkbmMWCustomService2.ReverseString(const AString:string;
         const AClientIdentity:TkbmMWClientIdentity):string;
begin
     Result:=StrUtils.ReverseString(AString);
end;

function TkbmMWCustomService2.AddNumbers(const AValue1,AValue2:integer;
         const ARemoteLocation:string):integer;
begin
     Result:=AValue1+AValue2;

     // ARemoveLocation contains the reported remote location
     // for the client.
end;

initialization
  // Make sure that RTTI is produced for the service class.
  TkbmMWRTTI.EnableRTTI(TkbmMWCustomService2);
复制代码

 

以上代码毫无疑问可以执行,但是最理想的是如何让他非常方便的增加新的函数,并且不用繁琐的加入
注册代码,并且让客户端访问。
可以通过调用kbmMWServer1.AutoRegisterServices来自动注册kbmMW_Service中定义的服务。

没有使用kbmMW_Arg绑定的声明自动的被看作声明值,并接受调用者传来的值。

例如 [kbmMW_Method] function EchoString(const AString:string):string; 与一下代码相同 [kbmMW_Method] function EchoString([kbmMW_Arg] const AString:string):string; 与一下代码也相同 [kbmMW_Method] function EchoString([kbmMW_Arg(mwatValue)] const AString:string ):string; 在客户端调用时的代码就如下:

    s:=client.Request('SMARTDEMO','','echostring',['abc']);
     s:=client.Request('SMARTDEMO','','EchoReversedString',['abc']);
     i:=client.Request('SMARTDEMO','','addnumbers',[10,20]);

还有更方便的客户端调用方法

复制代码
procedure TForm1.Button1Click(Sender: TObject);
var
   c:TkbmMWSmartClient;
   s:string;
   i:integer;
begin
     // New smart client.
     c:=TkbmMWSmartClientFactory.GetClient(Transport,'SMARTDEMO');
     s:=c.EchoString('abc');
     s:=c.EchoReversedString('abc');
     i:=c.AddNumbers(34,7);

     // Traditional client.
     s:=client.Request('SMARTDEMO','','echostring',['abc']);
     s:=client.Request('SMARTDEMO','','EchoReversedString',['abc']);
     i:=client.Request('SMARTDEMO','','addnumbers',[10,20]);
end;
复制代码

由于目前新版还没有发布,目前只有这些内容。

posted @ 2019-07-01 23:49  业容天  阅读(95)  评论(0编辑  收藏  举报