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>;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With