Dependency Injection in Swift
What is the use of dependency injection? | Sun 6 May 18
Dependency Injection in Swift you can avoid Singleton Pattern and make more testable code with loose coupling.
Initially, I also don’t know about Dependency Injection when I have followed some standard coding pattern then I realize should I need to go Dependency Injection. At the beginning stage no one perfect by the working on real-life projects and facing problems then we learn the new things.
Then I have started adding Dependency Injection inside my projects then I got understanding its pretty easy to use. Most of the place I have stared avoiding Singleton Pattern.
What is the use of dependency injection?
By using dependency injection we can make more testable, more simple code we can write. Only we need to pass dependency through object initialization. Every class has its own responsibility for injecting dependency through the object.
Let’s see the without dependency injection code -
This is the normal singleton class which we are using here.
class DataManager{
static let shared = DataManager()
var delegate: ResponseHandler?
private init(){}
func requestForData(){
//Code Process
...
}
}
How to use this class
class ViewController: UIViewController, ResponseHandler {
override func viewDidLoad() {
super.viewDidLoad() DataManager.shared.delegate = self
DataManager.shared.requestForData()
}}
With the use of Dependency Injection
class DataManager{
private var delegate: ResponseHandler //injecting here dependency
init(delegate: ResponseHandler) {
self.delegate = delegate
}
func requestForData(){
//Code Process
...
}
}
How to use this class
class HomeViewController: UIViewController, ResponseHandler {
//Use the class with self-executing closure
lazy var dataManager: DataManager = {
let manager = DataManager(delegate: self)
return manager
}() override func viewDidLoad() {
super.viewDidLoad()
}}
Passing dependency through the another ViewController
Now we have done small changes in current class.
class HomeViewController: UIViewController, ResponseHandler {
var dataManager: DataManager? override func viewDidLoad() {
super.viewDidLoad()
}}
coming this class from the another ViewController -
class ViewController: UIViewController{
//MARK: - Navigation
func moveToHomeController(){
guard let homeView =
self.storyboard?.instantiateViewController(withIdentifier:
"HomeViewController") as? HomeViewController
else { print("View controller not found")
return
}
//Passing here dependency
homeView.dataManager = DataManager(delegate: homeView.self)
navigationController?.pushViewController(homeView, animated:
true)
}
}
or through Segue
class ViewController: UIViewController{
//MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == homeViewControllerIdentifier,
let homeView = segue.destination as? HomeViewController {
homeView.dataManager = DataManager(delegate: homeView.self)
}
}
}
Dependency with Method injection
class APIManager{ func requestUserDetails(_ request: Request, database: DataManager, track: TrackDetails) -> ResponseManager? {
...
}
}
Conclusion
Here we got understanding for Dependency Injection for manage more testable and distributed inside your code. In this part, we have covered how to pass single dependency to multiple dependency injections.
One more important thing I have covered in this part for pass dependency through the one view controller to another view controller. It’s pretty easy to use inside your code.
We also got one more thing here how to get confirm protocol delegate inside your code.
Thanks for reading 🙌🏼
If you having any query regarding this tutorial ? Twitter me: @Anand