I will try to ask a question a second time. Do not blame me, please.
Situation:
I have a form
TfrmMain = class(TForm)
private
[Inject('IniFileSettings')]
FSettings: ISettings;
public
end;
I have a container initialization procedure:
procedure BuildContainer(const container: TContainer);
begin
container.RegisterType<TIniSettings>.Implements<ISettings>('IniFileSettings');
container.RegisterType<TfrmMain, TfrmMain>.DelegateTo(
function: TfrmMain
begin
Application.CreateForm(TfrmMain, Result);
end);
container.Build;
end;
So, I initialize both TfrmMain and TIniSettings through the container.
in.DPR I have:
begin
BuildContainer(GlobalContainer);
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end.
I also have an assistant for TApplication:
procedure TApplicationHelper.CreateForm(InstanceClass: TComponentClass; var Reference);
var
locator: IServiceLocator;
begin
locator := TServiceLocatorAdapter.Create(GlobalContainer);
if locator.HasService(InstanceClass.ClassInfo) then
TObject(Reference) := GlobalContainer.Resolve(InstanceClass.ClassInfo).AsObject
else
inherited CreateForm(InstanceClass, Reference);
end;
Problem: when I try
procedure TfrmMain.FormCreate(Sender: TObject);
begin
s := FSettings.ReadString('Connection', 'Server', 'localhost');
end;
I get an AV exception because FSettings is currently NIL.
What is the correct way to get an FSettings object from a container?
UPDATE:
FSettings := GlobalContainer.Resolve<ISettings>;
This line works fine ... Like last time, I have a problem using the [Inject] attribute. Even with a solution from Stefan, I can get this method to work:
How to initialize the main application form in Spring4D GlobalContainer?