How to implement Dependency Injection in Swift!

preview_player
Показать описание
🔥 Big thank you to Stream for sponsoring this video! 🔥

———

#iOS #swift #softwaredeveloper #iosdeveloper

Thank you for watching this video 🙌

(affiliate link)

Рекомендации по теме
Комментарии
Автор

Did you enjoy this introduction to Dependency Injection in Swift? Let me know in the comments 🚀

v_pradeilles
Автор

Thanks! Could be useful a new video explains how to manage dependency injections classes with a big project

ivan
Автор

Thoroughly explained, don't the objects in the dependency manager stay longer than needed?

nashtravelandlifestyle
Автор

Thanks Vincent for the video it does solve the problem of taking away the responsibility of the scene delegate to create the first ViewController. However I have a few questions with the Dependency provider structure which you have presented and would really like to get your view on my questions below.

1. Is there a better way to create Dependency provider ? .

2. Currently your DependencyProvider is a struct and it creates ViewModel and ViewController within it which I assume are a class. So in theory your struct is having class instances.

3. Is it really required for the struct to have static variables ? Can DependencyProvider just be a class with all the variables in it ?

deepeshnitk
Автор

Awesome video and love your channel. Just wanted to suggest another specific approach when injecting dependencies to storyboard view controllers.
Storyboard ViewControllers with dependency injection have a problem of breaking encapsulation due to the fact you have to keep your dependencies public thus risking it being overwritten from the outside at a later time.
This approach prevents that (though I honestly just prefer to use programmatic view controller to avoid the whole thing completely):

class ViewController: UIViewController {
//Can remain private since we are using inner static init.
private var someDependency: SomeDependency!

//Static init
static func instantiate(someDependency: SomeDependency) -> Self {
let vc = UIStoryboard(name: "Main", bundle: nil)
"Identifier") as! Self
vc.someDependency = someDependency
return vc
}

override func viewDidLoad() {
super.viewDidLoad()
//Use dependency here
}
}

MarkosDarkin
Автор

Hi, nice video. I often get asked the interview question. "Imagine if you have really large array what will be most appropriate way to handle it will you use value type or reference type?" I will really appreciate if you make a video addressing this. Merci bonne journée

HumbleHustle
Автор

Thanks, Vincent for this introduction, just wondering shouldn't the class TesService conform to the protocol Servicing and not inherit from class Service

byaruhaf
Автор

Hey, thanks for this video !
I wonder what's the implem when it comes to having a lot of VCs ? do you create multiples DependencyProvider ? what about navigation ?

imatricepte
Автор

actually, I don't think this does test the actual method which could still be being called twice. i doubt we would want to call the method `getData(_:)` directly in a unit test anyway assuming it's concurrent and hits an external dependency.

smilebot
Автор

XCTestExpectation seems more appropriate to use for checking that the viewModel calls the service only once

class MockedService: Servicing {

let expectation = "callCount")

func getData(_ completion: @escaping (Int) -> Void) {
expectation.fulfill()
}

}

class ViewModelTests: XCTestCase {

func testViewModelCallsService() {
let service = MockedService()
let vm = ViewModel(service: service)
vm.fetchData()
wait(for: [service.expectation], timeout: 1)
}

}

nomadromrom
Автор

How can this be done in SwiftUI ? or is dependency injection not needed in SwiftUI ?

priyacooking
Автор

Why is the DependencyProvider a struct and not a class?

Nairda