Dependency Injection in Swift

Batikan Sosun
Sahibinden Technology
3 min readMay 23, 2022

--

“Dependency Injection” is a 25-dollar term for a 5-cent concept. — James Shore

Photo by Clément Hélardot on Unsplash

Dependency Injection is a software design pattern that allows you to use a technique in which an object receives other objects that it depends on. These other objects are called dependencies.

Let’s make some sense of objects.

Receiving object is called a client and the passed (injected) object is called a service. The code that passes the service to the client can be many kinds of things and is called the injector. Instead of the client specifying which service it will use, the injector tells the client what service to use. The “injection” refers to the passing of a dependency (a service) into the object (a client) that would use it.

To implement the Dependency Injection pattern, we will use the Inversion of Control principle.

Inversion of Control is a programming principle. IoC inverts the flow of control as compared to traditional control flow. In IoC, custom-written portions of a computer program receive the flow of control from a generic framework.

To achieve the “inverting the flow of control” we have a technique. With this technique, we aim that construct all of the responsibilities of the class to its dependencies.

To make more readable, reusable code, maintainable code, and testable code, we can abstract the code, and the abstraction will provide a few types of implementation.

One more advantage of abstraction is that we can eliminate tightly coupled(decouple classes) relationships between classes. All the decoupled classes make it easy to do some stuff I mentioned above.

We can implement the Dependency Injection in several ways. These ways are Initializer Injection, Setter Injection(a.k.a Property Injection), and Interface Injection.

Initializer Injection is a most favorite way to inject dependencies, and also it’s my favorite solution to inject objects. You can also see it called Constructor Injection somewhere, both are completely the same.

In this way, we pass all the dependencies as initializer parameters.

At the first glance, we can easily understand what the class needs, and you see all the dependencies.

For example, let’s code to understand what’s going on.

In this example, we created a class named ViewModel. The ViewModel’s initializer method takes two arguments, and these arguments change two objects.

Both UserServiceProtocol and RewardServiceProtocol are services, and ViewModel is the client, as mentioned in the description above.

Using the protocols to make abstraction is a classic and easy way to make interchangeable implementations.

We can create an instance of ViewModel with data-mocked dependencies to run some unit tests.

Another dependencies injection way is Setter Injection(a.k.a Property Injection).

The Setter Injection technique needs a setter method to inject dependencies.

What did we, in the example above?
We created two setter methods, and these are taking the dependencies.

This way can be a good approach if you want a few dependencies, and some are optional.

This can be a pretty easy way to implement, but it’s easy to forget to inject a required dependency since these are optional.

The latest Dependencies Injection way is Interface Injection.

Interface Injection follows this way; the client conforms to protocols used to inject dependencies.

In this example, we created a protocol named ServiceProtocol that injects UserServiceProtocol and RewardServiceProtocol.
Also, this way provides more advantages than the second way. We can prefer that over second.
The ServiceProtocol completely doesn’t know the implementation.

By confirming the ServiceProtocol, the code gets loosely coupled.

And so far, I explained several ways to implement the Dependency injection pattern. I have to say that another important point is the Dependency Injection Container in the Implementation of the Dependency Injection Pattern.

The second part of the Dependency Injection series is here: Dependency Injection Container in Swift

Thanks for reading.

Let’s connect on Twitter

--

--

Batikan Sosun
Sahibinden Technology

Tweeting tips and tricks about #swift #xcode #apple Twitter @batikansosun Weekly Swift Blogging