Design Patterns · Senior · Question 19 of 75

Why is Service Locator often considered an anti-pattern compared to DI?

Explanation

locator.get('api') inside a function is an implicit dependency: a test has to set up a global registry, and errors show up at runtime. With DI, dependencies are visible in the parameters. useContext is close to a locator but is scoped to the tree and typed.

Further reading

Problem with a question

Design Patterns · Senior · question 19

Test yourself

Take the Design Patterns · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz