The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry known as the "service locator" which on request returns the information necessary to perform a certain task.
Questions tagged [service-locator]
424 questions
365
votes
16 answers
What's the difference between the Dependency Injection and Service Locator patterns?
Both patterns seem like an implementation of the principle of inversion of control. That is, that an object should not know how to construct its dependencies.
Dependency Injection (DI) seems to use a constructor or setter to "inject" it's…

Charles Graham
- 24,293
- 14
- 43
- 56
163
votes
10 answers
Is ServiceLocator an anti-pattern?
Recently I've read Mark Seemann's article about Service Locator anti-pattern.
Author points out two main reasons why ServiceLocator is an anti-pattern:
API usage issue (which I'm perfectly fine with)
When class employs a Service locator it is very…

davidoff
- 2,175
- 3
- 16
- 23
57
votes
7 answers
Accessing ASP.NET Core DI Container From Static Factory Class
I've created an ASP.NET Core MVC/WebApi site that has a RabbitMQ subscriber based off James Still's blog article Real-World PubSub Messaging with RabbitMQ.
In his article he uses a static class to start the queue subscriber and define the event…

Nick
- 4,115
- 10
- 45
- 57
50
votes
4 answers
Provider vs. Get_it
Searching for Dependency Injection solutions for Flutter, I found two awesome libraries: provider and get_it.
As far as I can see, provider has more boilerplate, but it fits really nicely with Flutter, allowing Consumer to rebuild parts of the…

Michel Feinstein
- 13,416
- 16
- 91
- 173
47
votes
2 answers
Why is MVC4 using the Service Locator Anti-Pattern?
After reading "Dependency Injection in .NET" by Mark Seemann I stay away from the Service Locator which is an anti-pattern.
Upon reading the release notes on MVC 4 I see:
Improved Inversion of Control (IoC) via DependencyResolver: Web API
now…

Tom Stickel
- 19,633
- 6
- 111
- 113
44
votes
11 answers
Dependency Injection vs Service Location
I am currently weighing up the advantages and disadvantages between DI and SL. However, I have found myself in the following catch 22 which implies that I should just use SL for everything, and only inject an IoC container into each class.
DI Catch…

Lawrence Wagerfield
- 6,471
- 5
- 42
- 84
43
votes
5 answers
Is the Service Locator pattern any different from the Abstract Factory pattern?
At first glance, the Service Locator pattern looks the same as the Abstract Factory pattern to me. They both seem to have the same use (you query them to receive instances of abstract services), and they both have been mentioned when I read about…

Merlyn Morgan-Graham
- 58,163
- 16
- 128
- 183
40
votes
9 answers
Which pattern to use for logging? Dependency Injection or Service Locator?
Consider this scenario. I have some business logic that now and then will be required to write to a log.
interface ILogger
{
void Log(string stuff);
}
interface IDependency
{
string GetInfo();
}
class MyBusinessObject
{
private…

CodingInsomnia
- 1,843
- 2
- 15
- 19
39
votes
4 answers
When would you use the Common Service Locator?
I've been looking at the Common Service Locator as a way of abstracting my IoC container but I've been noticing that some people are strongly against this type of this.
Do people recommend never using it? Always using it? or sometimes using it?
If…

ajma
- 12,106
- 12
- 71
- 90
20
votes
3 answers
Testable Controllers with dependencies
How can I resolve dependencies to a controller that is testable?
How it works: A URI is routed to a Controller, a Controller may have dependencies to perform a certain task.

user3121056
- 339
- 3
- 12
19
votes
2 answers
Is IDependencyResolver an anti-pattern?
I am designing some architectural changes into a legacy ASP.NET application. I prototyped some classes for dependency resolution that mimic the ASP.NET MVC's IDependencyResolver. I won't post because it is pretty much the same interface, but in…

michelpm
- 1,795
- 2
- 18
- 31
16
votes
5 answers
MVVM : how to pass parameter to ViewModel's constructor
I'm using L. Bugnion's MVVM Light Framework.
What are some of the recommended approaches to pass parameters such as Customer's ID to ViewModel's constructor?
Edit:
The parameter I need for each ViewModel is not something that is shared across…

Yeonho
- 3,629
- 4
- 39
- 61
15
votes
6 answers
Inject Koin in Java classes
I'm trying to replace Dagger 2 to Koin in my current project and I don't want to rewrite some classes in Kotlin to use it.
Is possible to inject with Koin in java classes?
In Kotlin is just
// Inject MyPresenter
val presenter : MyPresenter by…

Rafa Araujo
- 314
- 1
- 2
- 9
15
votes
2 answers
zf2 ServiceManager vs ServiceLocator
I am learning zf2, reading docs, tutorials etc from the web. Now I am confused regarding Zend\ServiceManager\ServiceManagerAwareInterface.php and Zend\ServiceManager\ServiceLocatorAwareInterface.php.
Also in some place (like controller) I use…

M Hill
- 225
- 2
- 6
15
votes
2 answers
Spring ServiceLocator or pure factory pattern?
99% of my dependency is manage with DI pattern via @Autowired Spring annotation.
Nevertheless in a particular scenario, I can't determine which implementation to be used until run-time.
The most known case, is the multiple implementation of…

Kakawait
- 3,929
- 6
- 33
- 60