Spring4D [inject] 属性在内部如何发挥作用?

How does the Spring4D [inject] attribute function internally?

我正在尝试创建一个最小示例,它与 Spring4D [inject] 属性的作用相同。它应该自动解析我的 TOrderAdapter.FDetailsAdapter,我想在 Factory 单元内手动实例化它(不像 Spring4D 容器那样工作,首先从外部注册接口)。 Factory 应该分发使用 [inject] 请求的任何所需接口。

很明显,我的代码无法工作(TOrderAdapter.FDetailsAdapter 没有被注入,在 ButtonClick 上给我一个 nil 指针访问冲突,第一次使用)。通读 Spring4D 源代码,我看不出这个 逻辑部分 在哪里,我缺少 以在我的示例中使用所需的功能.

program OrderDetails;

uses
  Vcl.Forms,
  Order.Adapter in 'Order.Adapter.pas',
  Details in 'Details.pas',
  Details.Adapter in 'Details.Adapter.pas',
  Factory.Adapter in 'Factory.Adapter.pas',
  Factory in 'Factory.pas',
  Order in 'Order.pas',
  Order.View in 'Order.View.pas' {OrderForm};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TOrderForm, OrderForm);
  Factory.Adapter.Factory := TFactoryAdapter.Create;
  Application.Run;
end.
unit Factory;

uses
  Rtti, TypInfo;

type
  InjectAttribute = class(TCustomAttribute)
  private
    fServiceType: PTypeInfo;
    fValue: TValue;
  public
    constructor Create(ServiceType: PTypeInfo); overload;
    property ServiceType: PTypeInfo read fServiceType;
    property Value: TValue read fValue;
  end;

implementation

constructor InjectAttribute.Create(ServiceType: PTypeInfo);
begin
  inherited Create;
  fServiceType := ServiceType;
end;

end.
unit Factory.Adapter;

uses
  Details, Details.Adapter, Order, Order.Adapter;

type
  TFactoryAdapter = class
  private
    FDetailsAdapter: IDetailsAdapter;
    FOrderAdapter: IOrderAdapter;
  public
    constructor Create;
    function Inject: IInterface; overload; // unused
  end;

var
  Factory: TFactoryAdapter;

implementation

constructor TFactoryAdapter.Create;
begin
  FDetailsAdapter := TDetailsAdapter.Create;
  FOrderAdapter := TOrderAdapter.Create;
end;

function TFactoryAdapter.Inject: IInterface; // unused
begin
  Result := FDetailsAdapter;
end;

end.
unit Details.Adapter;

uses
  Details, Winapi.Windows, SysUtils;

type
  TDetailsAdapter = class(TInterfacedObject, IDetailsAdapter)
  private
    FID: Integer;
  public
    procedure SetID(AID: Integer);
    function GetID: Integer;
  published
    property ID: Integer read GetID write SetID;
  end;

implementation

procedure TDetailsAdapter.SetID(AID: Integer);
begin
  FID := AID;
  OutputDebugString(PWideChar('OrderDetail ID set to ' + IntToStr(FID)));
end;

function TDetailsAdapter.GetID: Integer;
begin
  Result := FID;
end;

end.
unit Order.Adapter;

uses
  Order, Order.View, Details, Factory,
  Vcl.Forms;

type
  TOrderAdapter = class(TInterfacedObject, IOrderAdapter)
  private
    [inject]
    FDetailsAdapter: IDetailsAdapter;
  public
    constructor Create;
    procedure ButtonClick(Sender: TObject);
  end;

var
  OrderForm: TOrderForm;

implementation

constructor TOrderAdapter.Create;
begin
  OrderForm.Button1.OnClick := ButtonClick;
end;

procedure TOrderAdapter.ButtonClick(Sender: TObject);
begin
  FDetailsAdapter.ID := 5;
end;

end.

容器使用 RTTI 收集具有此属性的成员,并将正确的服务注入其中。