在运行注册DataSnap服务器类时发布provider

A while ago I published a blog post about How to register DataSnap Server Class in runtime with Delphi, after that I got some feedback mentioning the source code I provided just publishes the Server Methods, and not the Providers as part of the Server Module, in other words the client application couldn’t see the Providers available on the DataSnap Server.

The solution for this issue is very simple, we just need to register the TDSProviderDataModuleAdapter class, which is used internally to allow a TProviderDataModule to be exposed to DataSnap clients. TDSProviderDataModuleAdapter acts as a proxy between a server-side TProviderDataModule and client-sides TDSProviderConnection.

We could just update the GetDSClass method implementation as below, it works, but it would not be the best implementation, just because the following code will always publish the IAppServer interface methods to the client side, in case your ServerModule doesn’t have any Provider, why you will publish the IAppServer? Doesn’t make sense, right?

Making the publication parameterizable will bring more flexibility and control to the application and the developer, it will be your decision to publish the Providers or not. A new parameter in the TSimpleServerClass constructor is everything we need.

function TSimpleServerClass.GetDSClass: TDSClass;

1 begin
2   Result := TDSClass.Create(FPersistentClass, False);
3   Result := TDSClass.Create(TDSProviderDataModuleAdapter, Result)
4 end;

The following code represents the new implementation for the TSimpleServerClass. The change adds a new parameter (ExposeProvider) to the constructor, which will allow the developer to publish the providers (or not).

01 type
02  
03 unit SimpleServerClass;
04  
05 interface
06  
07 uses DSServer, Classes, DSCommonServer, DSReflect;
08  
09 type
10  
11   TSimpleServerClass = class(TDSServerClass)
12   private
13     FPersistentClass: TPersistentClass;
14     FExposeProvider : Boolean;
15   protected
16     function GetDSClass: TDSClass; override;
17   public
18     constructor Create(AOwner: TComponent; AServer: TDSCustomServer;
19       AClass: TPersistentClass; ExposeProvider: Boolean; ALifeCycle: String);
20       reintroduce; overload;
21   end;
22  
23 implementation
24  
25 constructor TSimpleServerClass.Create(AOwner: TComponent;
26   AServer: TDSCustomServer; AClass: TPersistentClass; ExposeProvider: Boolean;
27   ALifeCycle: String);
28 begin
29   inherited Create(AOwner);
30   FPersistentClass := AClass;
31   FExposeProvider  := ExposeProvider;
32   Self.Server := AServer;
33   Self.LifeCycle := ALifeCycle;
34  
35 end;
36  
37 function TSimpleServerClass.GetDSClass: TDSClass;
38 var
39   isAdapted : Boolean;
40 begin
41   isAdapted := FPersistentClass.InheritsFrom(TProviderDataModule);
42   Result := TDSClass.Create(FPersistentClass, isAdapted);
43   if FExposeProvider and isAdapted then
44      Result := TDSClass.Create(TDSProviderDataModuleAdapter, Result)
45 end;
46  
47 end.

The following code registers 3 classes, where only one exposes the Providers:

1 procedure RegisterServerClasses(AOwner: TComponent; AServer: TDSServer);
2 begin
3   Assert(AServer.Started = false'Can''t add class to non active Server');
4  
5   TSimpleServerClass.Create(AOwner, AServer, TGlobal, False, TDSLifeCycle.Server);
6   TSimpleServerClass.Create(AOwner, AServer, TCustomer, True, TDSLifeCycle.Session);
7   TSimpleServerClass.Create(AOwner, AServer, TObjectPool, False, TDSLifeCycle.Invocation);
8 end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值