Dependency Injection Container in Swift

Batikan Sosun
Sahibinden Technology
2 min readMay 28, 2022

--

Photo by Tudor Baciu on Unsplash

The first article of the Dependency Injection series is here. Dependency Injection in Swift

Dependency Injection is a significant pattern in the development lifecycle while working on the loosely coupled code.

As I mentioned in my previous article, if you intend to decouple the code, the Dependency Injection should be your first way in this journey.
There are many different approaches to Dependency Injection, and in this article, I’m going to deep dive into the concept called Dependency Injection Container.

A Dependency Injection Container is an object that is responsible for recording, deciding, and settling all the dependencies. Responsibilities of the deciding and settling mean that the DI container needs to know about the constructor arguments and the relationships between the objects.

Principally, deciding to use a dependency injection container can be the right way to handle numerous different objects with many dependencies.

Let’s deep dive into the code by creating an example. I’m going to make a protocol which is named AwesomeDICProtocol.

First of all, there are two main functions, and these two functions have an important task.
And then, I’m going to create a Dependency Injection Container class named AwesomeDIContainer, and that class will have responsibilities for handling the dependencies, such as creating and holding an object.

We have to make sure that our dependency injection container should be a singleton class. This singleton-approaching way prevents any forced use of multiple instances of the dependency injection container class and other unpredictable behavior, such as cutting some dependencies.

Let’s explain the steps in order;
The services property is a String-Any typed dictionary that holds all services. The dictionary’s String key represents the Service’s name, and the Any value refers to Service’s instance.
Service is a generic type. To prevent tightly coupled code, we will only use protocols. Such as UserServiceProtocol, RewardServiceProtocol, etc.(examples from the previous article)

In my previous article, which is the first of the Dependency Injection series, I have explained an example code below.

Now, I’m going to reimplement the initializer by Dependency Injection Container. AwesomeDIContainer class provides the necessary parameters by default. At this point, you can be sure that you can always give the dependencies for your testing purposes or register mocked dependencies in the container.

And finally, we are all set to use the Dependency Injection Container class. Before calling the ViewModel class, we have to register the Services we defined.
The Objects registration to the container will look like this:

Build and run. That’s what we wanted exactly.

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