Dependency Injections in Swift
By: Ashwin Kini
Tags: Apple, Dependency Injection, Design Patterns, Swift, iOS

We all as developers dream about writing codes that are hassle-free, loosely coupled and easily testable.

Initially, as beginners, we just try to put a code in place to accomplish the tasks and stay satisfied with it. But as we try to advance further, we may try to approach simpler things in a smart way by compelling ourselves to certain principles.

One such principle is Dependency injection!!!

What is Dependency Injection by the way??

It is just a fancy term used to demonstrate the concept of removing dependency from the object, thereby making the object more loosely-coupled.

So the better way of looking at it is, let the object continue to have its own responsibility without having to create the internal dependencies within it.

Rather dependencies are injected💉 to the object. This concept of injecting dependencies into an object is called Dependency Injection. Dependencies can be injected to an object through any of the below.

Property Injection Initialiser Injection Method Injection

Let us consider a case where the object creates the dependency with itself.

class PersonViewModel {

var person = Person()

}

Here the object PersonViewModel carries the burden of creating a dependency on Person object.

Now let us see how we can inject dependency to person object via a property

class PersonViewModel {

var person = Person?

} let personViewModel = PersonViewModel()

let person = Person()

personViewModel.person = person

Here the class PersonViewModel is now free from creating the dependency and uses the dependency injected via property. This method of injecting dependency via a property is called Property Injection.

Now let us see how we can inject dependency to person object via an initialiser

class PersonViewModel { var person = Person? init (person: Person) {

self.person = person

}

Here the initialiser takes the responsibility of injecting the dependency and with this, the PersonViewModel is fully initialised. This method of injecting dependency via initialiser is called Initialiser or Constructor Injection.

This is the most preferred method of dependency injection as the initializer usually tries to fully configure the object and is used as a dependency that doesn’t change during the lifetime of the dependent object

Lastly, let us see how we can inject dependencies via methods — implementing protocol methods or setter functions

protocol ResourceManager {

func processDetails(for person: Person)

} class PersonViewModel { ...... func setPerson(person: Person) {

self.person = person

}

...... }

Here too just like our previous two methods the dependency is externally injected, where the protocol takes it as a method parameter. This method of injecting dependency via method is called Method or Settler Injection.

So where is Dependency injection really useful?

Apart from creating readable code the major advantage here is it we who inject the dependencies to a loosely coupled piece of code.

This will help in the following scenarios

Help to mock classes — While passing instances of different classes to a chunk of code, it is easy to test the functionality and behaviour as we are injecting the dependency.

Help in unit testing — Unit testing involves testing the output of the code, for a given input and compare it with a predefined value. The modularity achieved by using DI plays a significant role as various class instances can be tested without having to replace any block of the code as the dependency is injected.

So that is basics of Dependency Injection in a nutshell. Hope you enjoyed reading

If you like this post, please click the clap 👏button below a few times to show your support!

For more articles, visit Nestedif Home page