2

Spring/4D フレームワークを使用して、アスペクトを認識したインターフェイスの依存関係が注入されたオブジェクトを作成したいと考えています。私の問題は、両方の部分を組み合わせる方法がわからないことです。一般的な考え方は次のとおりです。

  1. アスペクト レイヤー オブジェクトを作成し、2 つのインターフェイスを保持します。1 つは依存関係としてオブジェクトに渡し ( IAspect)、もう 1 つはアスペクトとしてオブジェクトに織り込みます ( IInterceptor)。

    Temp := TAspect.Create;
    Aspect := Temp as IAspect;
    Interceptor := Temp as IInterceptor;
    
  2. インターフェイス化された依存性注入オブジェクトを作成します。

    Instance := TInstance.Create(Aspect) {as IInstance};
    
  3. 側面を織ります:

    Result := TProxyGenerator.CreateInterfaceProxyWithTarget(Instance, [Interceptor]);
    

これを解決するために、次の行に沿ってファクトリをカスタム コンストラクターに登録することを考えています。

Aspect := Resolve<IAspect>;
Interceptor := Aspect as IInterceptor;
Instance := InstanceFactory(Aspect); // InstanceFactory := Resolve<IInstanceFactory>;
Result := TProxyGenerator.CreateInterfaceProxyWithTarget(Instance, [Interceptor]);

Container: TContainer私の問題は、これをSpringからどのように登録するのですか?


: 以下のプログラムは、私が望むように動作し、GetValue呼び出しが実行されるアスペクト レイヤーを示します。カスタム オブジェクトの作成$Regionは、メイン ルーチンで行われます。Spring/4D フレームワークの DI コンテナーを使用するために、このプログラムをどのようにリファクタリングする必要がありますか?

program Project1;

{$AppType Console}

{$R *.res}

uses
  System.SysUtils,
  Spring,
  Spring.Interception;

type
  IAspect = interface
    ['{AF8E19F6-176D-490E-A475-4682336CAB89}']
    function GetSetting: String;
    procedure SetSetting(const Value: String);

    property Setting: String read GetSetting write SetSetting;
  end;

  TAspect = class (TInterfacedObject, IInterceptor, IAspect)
  strict private
    FSetting: String;

    function GetSetting: String;
    procedure SetSetting(const Value: String);

    procedure Intercept(const Invocation: IInvocation);

  public
    constructor Create;
  end;

  IThingy = interface (IInvokable)
    function GetAspect: IAspect;
    function GetValue: String;
    procedure SetValue(const Value: String);

    property InstanceAspect: IAspect read GetAspect;
    property Value: String read GetValue write SetValue;
  end;

  TThingy = class (TInterfacedObject, IThingy)
  strict private
    FInstanceAspect: IAspect;
    FClassAspect: IAspect;
    FValue: String;

    function GetAspect: IAspect;
    function GetValue: String;
    procedure SetValue(const Value: String);

  public
    constructor Create(const InstanceAspect, ClassAspect: IAspect);
  end;

{ TAspect }

constructor TAspect.Create;
begin
  inherited;
  FSetting := ' intercepted by class aspect';
end;

function TAspect.GetSetting: String;
begin
  Result := FSetting;
end;

procedure TAspect.Intercept(
  const Invocation: IInvocation);
begin
  Invocation.Proceed;

  if Invocation.Method.Name = 'GetValue' then
    Invocation.Result := TValue.From<String>(Invocation.Result.AsString + FSetting);
end;

procedure TAspect.SetSetting(
  const Value: String);
begin
  FSetting := Value;
end;

{ TThingy }

constructor TThingy.Create(const InstanceAspect, ClassAspect: IAspect);
begin
  inherited Create;
  FInstanceAspect := InstanceAspect;
  FClassAspect := ClassAspect;
  FValue := 'Value';
end;

function TThingy.GetAspect: IAspect;
begin
  Result := FInstanceAspect;
end;

function TThingy.GetValue: String;
begin
  Result := FValue;
end;

procedure TThingy.SetValue(const Value: String);
begin
  FValue := Value;
end;

{ Main }

procedure Main;
var
  Temp: TInterfacedObject;
  ClassAspect: IAspect;
  ClassInterceptor: IInterceptor;
  InstanceAspect: IAspect;
  InstanceInterceptor: IInterceptor;
  Thingy1: IThingy;
  Thingy2: IThingy;
begin
  {$Region 'How to do this with the Spring DI container?'}
  Temp := TAspect.Create;
  ClassAspect := Temp as IAspect;
  ClassInterceptor := Temp as IInterceptor;

  Temp := TAspect.Create;
  InstanceAspect := Temp as IAspect;
  InstanceInterceptor := Temp as IInterceptor;
  Thingy1 := TThingy.Create(InstanceAspect, ClassAspect);
  Thingy1 := TProxyGenerator.CreateInterfaceProxyWithTarget(Thingy1, [ClassInterceptor, InstanceInterceptor]);

  Temp := TAspect.Create;
  InstanceAspect := Temp as IAspect;
  InstanceInterceptor := Temp as IInterceptor;
  Thingy2 := TThingy.Create(InstanceAspect, ClassAspect);
  Thingy2 := TProxyGenerator.CreateInterfaceProxyWithTarget(Thingy2, [ClassInterceptor, InstanceInterceptor]);
  {$EndRegion}

  Thingy1.InstanceAspect.Setting := ' intercepted by instance aspect 1';
  Thingy2.InstanceAspect.Setting := ' intercepted by instance aspect 2';

  Thingy1.Value := 'Value 1';
  Thingy2.Value := 'Value 2';

  WriteLn(Format('Thingy1.Value: %s', [Thingy1.Value]));
  WriteLn(Format('Thingy2.Value: %s', [Thingy2.Value]));
end;

begin
  try
    Main;
  except
    on E: Exception do
      WriteLn(E.ClassName, ': ', E.Message);
  end;
  if DebugHook <> 0 then
  begin
    WriteLn('Press enter...');
    ReadLn;
  end;
end.

出力:

Thingy1.Value: Value 1 intercepted by instance aspect 1 intercepted by class aspect
Thingy2.Value: Value 2 intercepted by instance aspect 2 intercepted by class aspect
Press enter...
4

1 に答える 1