In the following example, a service locator is used to inject dependencies over classes. Is that in any way better approach than resolving dependencies within the class and keep the initialiser (or class interface) clean?
class MyClass {
let service: Service
init(service: Service) {
self.service = service
}
}
class RootClass {
func something() {
let myClass = MyClass(service: ServiceLocator.shared.resolve())
}
}
This is one of the way to register and inject
}