当前位置:网站首页>Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance

Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance

2022-07-04 21:34:00 pcplayer

scene

WebService Server side , For the convenience of code management , Different business logic , Get different TSoapDataModule Go inside . This is good for modular management of code .

Multiple TSoapDataModule You may need to implement the same interface method , Every TSoapDataModule The implementation code of the module for this method may be different ( Because the business is different ); The simplest possibility is that each module has different client access rights to check .

that , On the client side , Because it is the same method that calls different interfaces on the server , If you write a method call to each interface , Code redundancy .

terms of settlement

Server side : Server side interface , Default (IDE Auto generated code ) from IAppServerSOAP Inherit .

If I want multiple interfaces with the same method ( Or functions ), You can define an interface by yourself , from IAppServerSOAP Inherit .

All interfaces on the server side , Inherit from a parent interface . The same interface method as above , Declare to this parent interface .

In this way , The client only needs a method to call the parent interface , Depending on the subclass interface , The actual implementation module of the server is different . Get it done !

The following code passed the test .

Code - Server side interface declaration

IParientIntf = interface(IAppServerSOAP)
    ['{04E35D2B-4A63-45DE-98C0-AED830BB06B7}']
    function Hello(const S: string): string; stdcall;
  end;

  //ISoapDM_A = interface(IAppServerSOAP)
  ISoapDM_A = interface(IParientIntf)
    ['{4499D900-1856-4178-8D6E-617856DBE2BD}']
  end;

  //ISoapDM_B = interface(IAppServerSOAP)
  ISoapDM_B = interface(IParientIntf)
    ['{9618B6D7-0826-47D7-AAF6-F7BB3D6F1CDE}']
  end;

Server side , Code of module 1 :

TSoapDM_A = class(TSoapDataModule, ISoapDM_A, IAppServerSOAP, IAppServer)
    FDConnection1: TFDConnection;
    FDQuery1: TFDQuery;
    DataSetProvider1: TDataSetProvider;
    DataSetProvider11111: TDataSetProvider;
  private
  
  public
    function Hello(const S: string): string; stdcall;
  end;
  
function TSoapDM_A.Hello(const S: string): string;
begin
  Result := 'Hello, ' + S + '; This is SoapDM_AAAAA';
end;

The service is end , Code of module 2 :

TSoapDM_B = class(TSoapDataModule, ISoapDM_B, IAppServerSOAP, IAppServer)
    FDConnection1: TFDConnection;
    FDQuery1: TFDQuery;
    DataSetProvider1: TDataSetProvider;
    DataSetProvider222222: TDataSetProvider;
  private
  
  public
    function Hello(const S: string): string; stdcall;
  end;
  
function TSoapDM_B.Hello(const S: string): string;
begin
  Result := 'Hello, ' + S + '; This is SoapDM_B';
end;

Client code :

procedure TForm3.DoHello(Intf: IParientIntf; const S: string);
var
  S2: string;
begin
  S2 := Intf.Hello(S);
  ShowMessage(S2);
end;

The client calls the server-side module 1 :

procedure TForm3.Button3Click(Sender: TObject);
begin

  Self.DoHello(HttpRIO1 as ISoapDM_A, ' Hello ');
end;

The client calls the server-side module 2 :

procedure TForm3.Button2Click(Sender: TObject);
begin
  Self.DoHello(HttpRIO2 as ISoapDM_B, ' Shenzhen ');
end;

Above call , Problems needing attention on the client :

The same HTTPRIO1 Called interface one , Then call interface 2 , There will be interface not support It's abnormal ; vice versa . Use the same HTTPRIO1 Call it successively 2 How to deal with different interfaces ? I haven't seen any method for the time being .

terms of settlement : Multiple HttpRIO example , Each instance corresponds to an interface .

therefore , In the above code , One is HttpRIO1 ; The other is HttpRIO2;

summary

Delphi Of WebService In the frame , The server side is made up of IDE Automatically created  TSoapDataModule Subclass module of , Has its own interface declaration . This interface , Inherited from  IAppServerSOAP;

We can declare an interface defined by ourselves , such as  IParientIntf = interface(IAppServerSOAP)

Then manually modify IDE Created code , The inherited parent class of the interface of the module , Change it to  IParientIntf;

In this way , It can simplify the code of the client , There is no need to correspond to each module , All implement the same calling code .

原网站

版权声明
本文为[pcplayer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042033575359.html