How to initialize main application form in Spring4D GlobalContainer?

后端 未结 1 1507
醉酒成梦
醉酒成梦 2020-12-09 23:14

so for instance I have a main form and want to inject a logger instance as private field.

I register the logger

GlobalContainer.RegisterType

        
相关标签:
1条回答
  • 2020-12-09 23:51

    You have to register your form to the container as well. This is done like this:

    procedure BuildContainer(const container: TContainer);
    begin
      container.RegisterType<ILogger, TCNHInMemoryLogger>;
      container.RegisterType<TForm8, TForm8>.DelegateTo(
        function: TForm8
        begin
          Application.CreateForm(TForm8, Result);
        end);
      container.Build;
    end;
    

    in your main you then write:

    begin
      BuildContainer(GlobalContainer);
      Application.Initialize;
      Application.MainFormOnTaskbar := True;
      frm_CNH := GlobalContainer.Resolve<Tfrm_CNH>;
      Application.Run;
    end.
    

    You could even write a helper for TApplication so you can keep the Application.CreateForm call and don't let the IDE mess up your main from time to time.

    type
      TApplicationHelper = class helper for TApplication
        procedure CreateForm(InstanceClass: TComponentClass; var Reference);
      end;
    
    procedure TApplicationHelper.CreateForm(InstanceClass: TComponentClass;
      var Reference);
    begin
      if GlobalContainer.HasService(InstanceClass.ClassInfo) then 
        TObject(Reference) := GlobalContainer.Resolve(InstanceClass.ClassInfo).AsObject
      else
        inherited CreateForm(InstanceClass, Reference);
    end;
    

    You then of course need to make sure your BuildContainer routine does not use that helper (put into a separate registration unit) or you end up in recursion.

    0 讨论(0)
提交回复
热议问题