Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

And get an instance via GlobalContainer or ServiceLocator, both of them work:

MyInstance := GlobalContainer.Resolve<IMyInterface>;
MyInstance := ServiceLocator.GetService<IMyInterface>;
like image 520
Rafael Piccolo Avatar asked Feb 07 '13 02:02

Rafael Piccolo


1 Answers

The ServiceLocator is for resolving dependencies in your code when needed. There you don't want to use a reference to the container as that would be totally against the purpose of having losely coupled code.

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

like image 169
Stefan Glienke Avatar answered Oct 18 '22 12:10

Stefan Glienke