Design Patterns · Senior · Question 19 of 75
Why is Service Locator often considered an anti-pattern compared to DI?
- ✓Correct answer: Dependencies are hidden: they aren't visible in the signature
- 2Service Locator works only on the server
- 3Service Locator is slower because of lookups by string keys
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.