Spring4D ServiceLocation at Designtime

Hi all. I want to build a component that requires database connectivity (NexusDB) at design time (as well as runtime). I’ve implemented an interface IBeehiveDatabase that works incredibly well as follows (project file) at runtime.

  ...
  Beehive.Interfaces,
  Spring.Container,
  Spring.Services,
  Beehive.Database in '..Beehive.Database.pas', { implements IBeehiveDatabase }
  ...
var
  FDatabase: IBeehiveDatabase;
  ...
begin
  GlobalContainer.Build;
  Application.Initialize;
  FDatabase := ServiceLocator.GetService<IBeehiveDatabase>;
  if not FDatabase.Connect(['beehiveclient', 'beehiveglobal']) then
    Application.Terminate;

The component I want to build is for retrieving SVG data. This component may use the service locator internally to get IBeehiveDatabase or it may be passed in from somewhere else. Wherever it comes from, where or how do I execute GlobalContainer.Build at designtime?
Thanks, Gerard Hook

Dependency injection and service location are not designed to work for RAD environments - they are a runtime construct.

Unless you have a design time component that builds a container - it’s not going to happen.

Create an Active or Connect property.
When it’s set to true, call your Build function.

Thanks Chee.

I have tried this and it works. I’m questioning if I should be doing this.

It would be good if there was a dedicated Spring4D Telegram channel.

Gerard Hook

@sglienke has said the Google Group is his main forum for discussion :
Spring4D - Google Groups https://share.google/rzHdj1LG67BZA6p5r

Thanks Paul.

I’ve just re-read Nick’s Dependency Injection/ Spring4D section in ‘Coding In Delphi’ for further understanding.

Gerard Hook

1 Like