我想将接口注入到对象中,但找不到属性[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 为零。当我使用 FStorage:= ServiceLocator.GetService; 然后在构造函数中分配 FStorage。但我想使用注入而不是 ServiceLocator。如果这是可能的。