When we are working to create a DataSnap application we need to register the Server classes in order to provide access to the client application to the server methods. The natural way is to use the DSServerClass component, but some time we want do it in run time. The following code shows how to do that, the TSimpleServerClass inherit from TDSServerClass and overload/reintroduce the Create method, adding the parameters ServerClass, DataSnap Server and LifeCycle.
type TSimpleServerClass = class(TDSServerClass) private FPersistentClass: TPersistentClass; protected function GetDSClass: TDSClass; override; public constructor Create(AOwner: TComponent; AServer: TDSCustomServer; AClass: TPersistentClass; ALifeCycle: String); reintroduce; overload; end; procedure RegisterServerClasses(AOwner: TComponent; AServer: TDSServer); implementation constructor TSimpleServerClass.Create(AOwner: TComponent; AServer: TDSCustomServer; AClass: TPersistentClass; ALifeCycle: String); begin inherited Create(AOwner); FPersistentClass := AClass; Self.Server := AServer; Self.LifeCycle := ALifeCycle; end; function TSimpleServerClass.GetDSClass: TDSClass; begin Result := TDSClass.Create(FPersistentClass, False); end;
Now we just need to instantiate the TSimpleServerClass for each server class.
The following code register 3 classes on the same server using different LifeCycles.
procedure RegisterServerClasses(AOwner: TComponent; AServer: TDSServer); begin Assert(AServer.Started = false, 'Can''t add class to non active Server'); TSimpleServerClass.Create(AOwner, AServer, TGlobal, TDSLifeCycle.Server); TSimpleServerClass.Create(AOwner, AServer, TCustomer, TDSLifeCycle.Session); TSimpleServerClass.Create(AOwner, AServer, TObjectPool, TDSLifeCycle.Invocation); end;
Simple and useful.
Related Posts
Andreano Lanusse | Technology and Software Development
Follow me on Twitter: @andreanolanusse