November 2018
Intermediate to advanced
346 pages
8h 12m
English
First, a definition—Service locator is a software design pattern that revolves around an object that acts as a central repository of all dependencies and is able to return them by name. You'll find this pattern in use in many languages and at the heart of some DI frameworks and containers.
Before we dig into why this is DI induced damage, let's look at an example of an overly simplified service locator:
func NewServiceLocator() *ServiceLocator { return &ServiceLocator{ deps: map[string]interface{}{}, }}type ServiceLocator struct { deps map[string]interface{}}// Store or map a dependency to a keyfunc (s *ServiceLocator) Store(key string, dep interface{}) { s.deps[key] = dep}// Retrieve a dependency by keyfunc (s *ServiceLocator ...
Read now
Unlock full access