Mallow's Blog

An Introduction for Dependency Injection in Swift

Dependency injection is a technique wherein one object will supply the dependencies of another object. To be more detail, dependency is an object that can be used, like a service and an injection defines, passing a dependency to a dependent object (a client/service) that would use it.

Dependency Injection is mainly used with the intention of writing a code that is loosely coupled, thus, making it easier to test. When we use dependency injection in our code, we are essentially giving an object its instance variables.

Here is a very basic example, that distinguishes default methodology and injection properties,

In this example, we are defining a UIViewController class that declares a property, named requestManager, which is of type RequestManager? (Optional type)

import UIKit

class ViewController: UIViewController {

    var requestManager: RequestManager?

}


We can initialise the value of the requestManager property using the following,

Without Dependency Injection

This means to populate the ViewController class with the instantiation of the RequestManager instance. In this way, the view controller is in charge of creating the RequestManager instance.

import UIKit

class ViewController: UIViewController {

    var requestManager: RequestManager? = RequestManager()

}


This means, the ViewController class not only knows about the behaviour of the RequestManager class but also knows about its instantiation. 

With Dependency Injection

Using dependency injection, we can inject the RequestManager instance into the ViewController instance. All though the end result may appear identical, it isn’t. By injecting the request manager instance object, the view controller doesn’t know how to instantiate the request manager.

// Initialize View Controller
let viewController = ViewController()

// Configure View Controller
viewController.requestManager = RequestManager()


Many developers mainly ignore the second option (with dependency injection) because it’s cumbersome and unnecessarily complex. But if we consider the benefits, then dependency injection becomes more appealing.

Main benefits of this approach includes,

1. Loose coupling :

It mainly makes the components less coupled and more reusable in different contexts coupling of objects especially in case of protocols.

2. Improve Testability :

Unit testing is much easier while using dependency injection. It allows developers to replace an object’s dependencies with mock objects, which makes isolating behaviour and setting up unit tests easier and less complicated.

3. Improves Transparency :

The responsibilities and requirements of a class or structure become more clear and transparent. By injecting a manager into a view controller, it is clear that the view controller depends on the request manager, providing better encapsulation.

There are few types of dependency injection approaches particular to the usages like, property injection, constructor injection etc. We will deal with its types and mocking practices in the next blog.


Poorvitha Y,
iOS Development Team,
Mallow Technologies.

Leave a Reply

%d bloggers like this: