Dependency Injection in iOS apps using Swift 5

Sayed Obaid
3 min readAug 21, 2021

--

What’s Dependency Injection?

Dependency Injection is like a scarecrow, Seeing it from far away makes you feel like it’s a very complicated thing, however when you get close enough you’ll find that it’s not that scary!
The big term “Dependency Injection” or simply “DI” is a programming technique that makes a class independent of its dependencies by passing (Injecting) a dependency into the client that uses it mostly through the constructor.

Why should you use Dependency Injection in your projects?

Using Dependency Injection techniques in your apps/projects will give you lots of benefits, Such as:
1- Reusability
2- Maintainability
3- Scalability
4- Testability

And for instance, If we have a set of classes that interact with each other and you need to make changes in one of them, how many changes you will need to do in the other classes in order to make it work?. the more changes you’ll need to make the more tight coupled your system is, and the lower changes you’ll need to make the means that your system is loose coupled. Dependency injection helps you to write a more loose coupled code which will eventually makes the code changes easier and maintainable.
Also, DI will increase your system’s testability as you can easily mock the service that you’re injecting instead of using the real service, the mocked service can be passed to the initializer to provide the expected responses and test your functionality

Now let’s get our hands dirty with the code

Imagine that we’re working with some modules that links pets to their human owners, we can probably write something like this

The problem with that code is that the Human and the Cat classes are tight coupled, a Human will always have a Cat as pet and the cat will always have a Human as an owner, now Imagine if you want to create an instance from Human that has a Dog, You’ll need to make hard changes to the code, and thats why tight coupling is so bad.

In order to make it less coupled we can use protocols as following

We have made the classes less coupled by introducing protocols which changed the concrete type Cat to an abstract type Pet. So that, the relationship between classes became more loose, A human has a pet that can be anything that conforms to the Pet protocol, However this code is not the best thing we can have, it’s not fully decoupled from Cat yet, as we defined Pet as a protocol, however we are stating what the pet is inside the Human class, and we want to make it as generic as possible because each human has a different pet, and here’s where Dependency Injection magic comes in.

The previous code ensures that Human and Cat are independent of each other, A human can have any pet that conforms to the Pet protocol, and a Cat can have any owner that conforms to the PetOwner protocol, now the question is: if you want to create 3 instances of Human, how can you initialize the pet for each one?
Dependencies can be injected into objects by many means (such as constructor injection or setter injection) and here’s an example of constructor injection

and Finally you can create as many Human objects as you want with different pet type, it can be a Cat, a Dog, or even a Kangaroo as long as it conforms to the Pet protocol without changing a single line of code.

Conclusion

Dependency Injection is a software engineering technique and mindset that can be used with any development language/framework, It makes the code more reusable, testable, and maintainable.

Please feel free to write all of your comments and feedback.

--

--

Sayed Obaid

Senior iOS developer with a huge interest in Software Engineering, Machine Learning, and Tech Industry