Я хочу, чтобы ввести интерфейс в объект, но я не могу найти проблему атрибута [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) Я хочу, чтобы заменить с поиска сервиса инъекции поля, но я не могу найти, как это сделать.
Некоторые примеры, но это не работает. Я не вижу проблемы.
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 во время выполнения, FStorage в TMyButton является Нилл. Когда я использую FStorage: = ServiceLocator.GetService; в конструкторе тогда назначается FStorage. Но я хочу использовать инъекцию, а не ServiceLocator. Если это возможно.
FStorage - это нуль, когда я использую атрибут injection – Ravaut123