How to implement Dependency Injection in Swift!

  Рет қаралды 14,001

Vincent Pradeilles

Vincent Pradeilles

Күн бұрын

Пікірлер: 29
@v_pradeilles
@v_pradeilles 3 жыл бұрын
Did you enjoy this introduction to Dependency Injection in Swift? Let me know in the comments 🚀
@JeannChuab
@JeannChuab Жыл бұрын
As always a great video, super objective and clear. Thank you Vincent!
@coolcodingdad
@coolcodingdad 2 ай бұрын
Thank you, Vincent! Great video!
@ivan180593
@ivan180593 3 жыл бұрын
Thanks! Could be useful a new video explains how to manage dependency injections classes with a big project
@fredericadda
@fredericadda 3 жыл бұрын
Great explanation, thanks Vincent!
@MarkosDarkin
@MarkosDarkin 2 жыл бұрын
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) .instantiateViewController(withIdentifier: "Identifier") as! Self vc.someDependency = someDependency return vc } override func viewDidLoad() { super.viewDidLoad() //Use dependency here } }
@pradipdeore8068
@pradipdeore8068 2 жыл бұрын
Thanks Vincent, great explanation.
@nashtravelandlifestyle
@nashtravelandlifestyle Жыл бұрын
Thoroughly explained, don't the objects in the dependency manager stay longer than needed?
@v_pradeilles
@v_pradeilles Жыл бұрын
Good question! In my example all the properties of my DependencyProvider are computed properties, so they don't store anything and produce a new value each time they're called. However, this is a simplified implementation. Dependency Injection frameworks, like Swinject, will indeed typically implement a mechanism to manage the lifetime of stored dependencies 👍
@nashtravelandlifestyle
@nashtravelandlifestyle Жыл бұрын
@@v_pradeilles thank you for clearing it out ☺️
@byaruhaf
@byaruhaf 3 жыл бұрын
Thanks, Vincent for this introduction, just wondering shouldn't the class TesService conform to the protocol Servicing and not inherit from class Service
@v_pradeilles
@v_pradeilles 3 жыл бұрын
Good question! Here the interest of subclassing Service is that we can call super, getting along the way a working implementation for free. But in a real project that calls live web services, it would indeed make sense to create a test instance that would directly implement Servicing and provide its own mocked implementation.
@imatricepte94
@imatricepte94 3 жыл бұрын
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 ?
@v_pradeilles
@v_pradeilles 3 жыл бұрын
For a big project, I would recommend looking at a framework like Swinject that's going to help you split and manage the code that resolves dependencies 👉 github.com/Swinject/Swinject
@deepesh259nitk
@deepesh259nitk Жыл бұрын
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 ?
@danielniels22
@danielniels22 10 ай бұрын
i think he was using struct because as the term "provider" that was only called and needed once. i see it as a one-time use, and the struct will then be destroyed in memory (since struct is a value type)
@HumbleHustle101
@HumbleHustle101 Жыл бұрын
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
@danielniels22
@danielniels22 10 ай бұрын
hi this is an interesting problem. have you figured out the explanation? im also preparing for my interview 🙏
@siddharthkothari007
@siddharthkothari007 2 жыл бұрын
nice explanation.
@watafakaya
@watafakaya Жыл бұрын
thank you, the best
@priyacooking
@priyacooking Жыл бұрын
How can this be done in SwiftUI ? or is dependency injection not needed in SwiftUI ?
@v_pradeilles
@v_pradeilles Жыл бұрын
Great question! SwiftUI actually had a built in implementation of Dependency Injection, which is called the “Environment” 👌
@priyacooking
@priyacooking Жыл бұрын
@@v_pradeilles thats nice.
@Nairda1705
@Nairda1705 2 жыл бұрын
Why is the DependencyProvider a struct and not a class?
@v_pradeilles
@v_pradeilles 2 жыл бұрын
That’s a good remark! As you can see, DependencyProvider only has static members, and so we never need to instantiate it. So we could make it easier a class, a struct or an enum: it wouldn’t change anything.
@smilebot484
@smilebot484 2 жыл бұрын
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.
@v_pradeilles
@v_pradeilles 2 жыл бұрын
Yeah I agree that in a real test, our mock wouldn't call the real implementation and instead would provide static data.
@nomadromrom
@nomadromrom 3 жыл бұрын
XCTestExpectation seems more appropriate to use for checking that the viewModel calls the service only once class MockedService: Servicing { let expectation = XCTestExpectation(description: "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) } }
@fabriziopastor6146
@fabriziopastor6146 11 ай бұрын
Thanks Vincent, great explanation.
How to implement the MVVM architecture in Swift!
8:33
Vincent Pradeilles
Рет қаралды 10 М.
Dependency Injection | Prime Reacts
28:34
ThePrimeTime
Рет қаралды 368 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
Dependency Injection
15:31
SwiftBook
Рет қаралды 15 М.
Dependency injection в Swift.
25:35
iOS Skills
Рет қаралды 2,4 М.
Swinject: Effortless Dependency Injection in iOS (Basics)
12:45
iOS Academy
Рет қаралды 30 М.
Swift Dependency Injection - What is it? What are the benefits?
6:58
Dependency Injection in Swift
6:44
AF Swift Tutorials
Рет қаралды 2,5 М.
Кирилл Володин - Как выстроить DI в многомодульном iOS-приложении при помощи Needle
42:01
Mobius — конференция по мобильной разработке
Рет қаралды 2,1 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 912 М.
Learn Dependency Injection and Write Better Code
21:52
Amigoscode
Рет қаралды 179 М.
Путин ответил на ультиматум Трампа
7:25
Diplomatrutube
Рет қаралды 1,6 МЛН
Dependency Injection in 8 minutes (Swift 2023)
9:09
CodeBrah
Рет қаралды 3,2 М.