たとえば、次のコードがあるとします。
interface
type
IMyInterface1 = interface // GUID
procedure ButtonEvent(Sender: TObject);
end;
IMyInterface2 = interface // GUID
procedure DoSomething;
end;
TMyClass1 = class(TInterfacedObject, IMyInterface1)
public
procedure ButtonEvent(Sender: TObject);
end;
TMyClass2 = class(TInterfacedObject, IMyInterface2)
public
procedure DoSomething;
end;
// ...
implementation
procedure TMyClass1.ButtonEvent(Sender: TObject);
var
aIntf2: TMyInterface2;
begin
// Pseudo code:
// aIntf2 := ServiceLocator.GetService<IMyInterface2>;
try
aIntf2.DoSomething;
finally
aIntf2 := nil; // will free the instance...
end;
end;
initialization
// Pseudo code:
// GlobalContainer register IMyInterface1 / TMyClass1
// GlobalContainer register IMyInterface2 / TMyClass2
// GlobalContainer.Build
end.
メソッド ButtonEvent は、Delphi フォームのボタン クリック イベントによって呼び出されます。
今私の質問: クラス TMyClass2 をインスタンス化するより良い方法はありますか? 私の場合、クラス TMyClass1 への挿入は不可能です。TMyClass2 インスタンスの有効期間は ButtonEvent 内のみです。ButtonEvent への次の呼び出しでは、別のインスタンスを使用する必要があります...
私の知る限り、メソッドパラメーターの注入またはローカル変数の注入はSpring4Dでは不可能ですよね?