3

オブジェクトにインターフェースを注入したいが、属性の問題が見つからない [Inject]

何が機能していますか。

unit uStorage;

interface

uses
  uStorageInterface;

type
  TStorageService = class (TInterfacedObject, IStorageService)
  private
    FPath: String;
    procedure SetPath(const Value: String);
    function GetPath: String;
   public
     property Path: String read GetPath write SetPath;
   end;

implementation

{ TStorageService }

function TStorageService.GetPath: String;
begin
  Result:= FPath;
end;

procedure TStorageService.SetPath(const Value: String);
begin
  FPath := Value;
end;

unit uStorageInterface;

interface

type
   IStorageService = interface
     ['{F1B4C339-BE8E-4182-A191-95266160FA6E}']
     procedure SetPath(const Value: String);
     function GetPath: String;
     property Path: String read GetPath write SetPath;
   end;

   IStorageObject = interface
     ['{7B97B659-EDF3-4892-AFAB-985487660372}']
   end;

implementation

end.

unit uObjects;

interface

uses
  Vcl.StdCtrls,
  System.Classes,
  uStorageInterface,
  Spring.Container,
  Spring.Services,
  Spring.Container.Common;

type

  TMyButton= class (TButton, IStorageObject)
  private
    FStorage: IStorageService;
    function GetStorage: IStorageService;

  protected
    procedure DoExit; override;

  public
    constructor Create(AOwner: TComponent); override;
  end;

implementation

{ TMyButton }
constructor TMyButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FStorage:= ServiceLocator.GetService<IStorageService>;
end;

procedure TMyButton.DoExit;
begin
  inherited;
    if assigned(FStorage) then
  begin
    self.Caption:= FStorage.Path;
  end;
end;

function TMyButton.GetStorage: IStorageService;
begin
  Result:= FStorage;
end;

end.

unit Unit2;

interface

uses
  System.SysUtils, System.Classes,
  Vcl.Dialogs;

type
  TDataModule2 = class(TDataModule)
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DataModule2: TDataModule2;

implementation

uses
  uStorage,
  uObjects,
  uStorageInterface,
  Spring.Services,
  Spring.Container;

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

procedure TDataModule2.DataModuleCreate(Sender: TObject);
begin
  GlobalContainer.RegisterType<TStorageService>.Implements<IStorageService>.DelegateTo(
    function: TStorageService
    begin
      Result := TStorageService.Create();
      Result.Path:= 'MyButton';
    end).AsSingleton;
  GlobalContainer.Build;
end;
end.

コンストラクタ TMyButton.Create(AOwner: TComponent) で ServiceLocator をフィールド インジェクションに置き換えたいのですが、これを行う方法が見つかりません。

いくつかの例ですが、うまくいきません。問題がわかりません。

unit uObjects;

interface

uses
  Vcl.StdCtrls,
  System.Classes,
  uStorageInterface,
  Spring.Container,
  Spring.Services,
  Spring.Container.Common;

type

  TMyButton= class (TButton, IStorageObject)
  private
    [Inject]
    FStorage: IStorageService;
    function GetStorage: IStorageService;

  protected
    procedure DoExit; override;

  public
    constructor Create(AOwner: TComponent); override;
  end;

implementation

{ TMyButton }
constructor TMyButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  //FStorage:= ServiceLocator.GetService<IStorageService>;
end;

procedure TMyButton.DoExit;
begin
  inherited;
    if assigned(FStorage) then
  begin
    self.Caption:= FStorage.Path;
  end;
end;

function TMyButton.GetStorage: IStorageService;
begin
  Result:= FStorage;
end;

end.

procedure TDataModule2.DataModuleCreate(Sender: TObject);
begin
  GlobalContainer.RegisterType<TStorageService>.Implements<IStorageService>.DelegateTo(
    function: TStorageService
    begin
      Result := TStorageService.Create();
      Result.Path:= 'MyButton';
    end).AsSingleton;

  GlobalContainer.RegisterType<TMyButton>.Implements<IStorageObject>.InjectField('FStorage');
  GlobalContainer.Build;
end;

実行時に TMyButton を作成すると、TMyButton の FStorage は nill です。FStorage:= ServiceLocator.GetService; を使用する場合 コンストラクターでは、FStorage が割り当てられます。しかし、ServiceLocator ではなくインジェクションを使用したいと考えています。これが可能であれば。

4

1 に答える 1