What is the difference between GlobalContainer and ServiceLocator in Spring for Delphi?

They seem so similar. I can register something in GlobalContainer:

GlobalContainer.RegisterType<TMyImplementation>.Implements<IMyInterface>; 

And get the instance through GlobalContainer or ServiceLocator , both of them work:

 MyInstance := GlobalContainer.Resolve<IMyInterface>; MyInstance := ServiceLocator.GetService<IMyInterface>; 
+4
source share
1 answer

ServiceLocator designed to resolve dependencies in your code if necessary. You don’t want to use a container reference there, as that would be completely against the goal of having code linked.

Personally, I agree with those who say that the service locator itself is an anti-pattern, and should be avoided whenever possible by injecting everything that is possible.

+6
source

All Articles