2014年5月12日星期一

我写的一个注册服务方法的Unit

先定义一个基础服务方法类
unit uServerMethod;

interface

uses
  System.SysUtils, System.Classes,
  Datasnap.DSProviderDataModuleAdapter,
  Datasnap.DSServer, Datasnap.DSCommonServer, Datasnap.DSReflect;

type
  {$METHODINFO ON}
  TServerMethod = class(TPersistent)
  public
    constructor Create; virtual;
    destructor Destroy; override;
  end;
  {$METHODINFO OFF}

  TServerMethodClass = class of TServerMethod;

procedure RegisterServerMethodClass(AClass: TServerMethodClass);
procedure RegisterServerMethodClasss(AOwner: TComponent; AServer: TDSServer);

implementation

uses
  uLog;

type
  TDSServerClass = class(Datasnap.DSServer.TDSServerClass)
    private
      FServerMethodClass: TServerMethodClass;
    protected
      function GetDSClass: TDSClass; override;
      procedure CreateInstance(const CreateInstanceEventObject: TDSCreateInstanceEventObject); override;
      procedure DestroyInstance(const DestroyInstanceEventObject: TDSDestroyInstanceEventObject); override;
    public
      constructor Create(AOwner: TComponent; AServer: TDSCustomServer; AClass: TServerMethodClass); reintroduce; overload;
      destructor Destroy; override;
    end;

var
  ServerMethodList: TList;
procedure RegisterServerMethodClass(AClass: TServerMethodClass);
begin
  ServerMethodList.Add(AClass);
end;

procedure RegisterServerMethodClasss(AOwner: TComponent; AServer: TDSServer);
var
  I: Integer;
begin
  for I := 0 to ServerMethodList.Count - 1 do
  begin
    TDSServerClass.Create(AOwner, AServer, ServerMethodList[I]);
  end;
end;

{ TDSServerClass }

constructor TDSServerClass.Create(AOwner: TComponent; AServer: TDSCustomServer;
  AClass: TServerMethodClass);
begin
  inherited Create(AOwner);
  FServerMethodClass := AClass;
  Self.Server := AServer;
end;

procedure TDSServerClass.CreateInstance(
  const CreateInstanceEventObject: TDSCreateInstanceEventObject);
var
  AObject: TServerMethod;
begin
  inherited;
  if CreateInstanceEventObject.ServerClassInstance = nil then
  begin
    CreateInstanceEventObject.ServerClassInstance := FServerMethodClass.Create; //不要让TDSClass.CreateInstance去创建
  end;
end;

destructor TDSServerClass.Destroy;
begin
  inherited;
end;

procedure TDSServerClass.DestroyInstance(
  const DestroyInstanceEventObject: TDSDestroyInstanceEventObject);
begin
  inherited;

end;

function TDSServerClass.GetDSClass: TDSClass;
begin
  Result := TDSClass.Create(FServerMethodClass, False);
end;

{ TServerMethod }

constructor TServerMethod.Create;
begin
  log.Debug('%s.Create', [ClassName]);
end;

destructor TServerMethod.Destroy;
begin
  log.Debug('%s.Destroy', [ClassName]);
  inherited;
end;

initialization
  ServerMethodList := TList.Create;
finalization
  FreeAndNil(ServerMethodList);

end.
然后就可以开始继承实现做应用了,写个例子
unit sSample;

interface

uses
  uServerMethod;

type
  TSmSample = class(TServerMethod)
  public
    function EchoString(Value: string): string;
    function ReverseString(Value: string): string;
  end;

implementation

{ TSmSample }

uses
  System.StrUtils;

function TSmSample.EchoString(Value: string): string;
begin
  Result := Value;
end;

function TSmSample.ReverseString(Value: string): string;
begin
  Result := System.StrUtils.ReverseString(Value);
end;

initialization
  RegisterServerMethodClass(TSmSample);

finalization

end.
以后关注服务方法实现就可以了。

没有评论:

发表评论