Dependency Injection Example
Step 1 : Regist instance with protocol
Configurator.shared.regist(type: SampleInteractorPtorocol.self, instance: SampleInteractor())
Step2 : Define protocol, Inject the instance and use it
protocol SampleInteractorPtorocol {
func doSomething()
}
class ViewController: UIViewController {
lazy var interactor = Configurator.shared.inject(type: SampleInteractorPtorocol.self)
override func viewDidLoad() {
super.viewDidLoad()
interactor?.doSomething()
}
}
Step3 : Implements the protocol
class SampleInteractor : SampleInteractorPtorocol {
func doSomething() {
print("doing something")
}
}
Useful for clean-swift architecture, a.k.a VIP