From the course: Swift: Writing Testable Code

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

What is dependency injection (DI)?

What is dependency injection (DI)?

From the course: Swift: Writing Testable Code

What is dependency injection (DI)?

- [Voiceover] So we're on board with making our apps more testable, but how can we do that? Tests will be easier to write if we can separate our classes from each other and examine them individually, but that's not possible all the time. To help us with this problem, let's take a look at something called the Dependency Inversion Principle. In its essence, it serves to decouple classes from one another by explicitly providing dependencies for them, rather than having them create the dependencies themselves. Dependencies in this context refers to the other classes that a class needs in order to accomplish a task. For example, a class making a web request might have a dependency on URLSession. Instead of having the class create the instance of a dependency, we can pass that dependency in through the initializer. Thus we are injecting the class's dependencies when we create them. So if we had, for example, a PhotoDownloader class, it might have a dependency on URLSession and URLCache…

Contents