LoginSignup
2
1

More than 5 years have passed since last update.

Dependency Injection - Swift

Posted at

Introduction

Today topic that we are going to cover is , Dependency Injection and we are going to discuss about
1. What is dependency injection?
2. Type of dependency injection?
3. Why do we use dependency injection?

ok. Now let's get started.

1. What Is Dependency Injection?

Base on James Shore definition Dependency injection means giving an object its instance variables. Really. That's it. It is hard to understand right? Ok so to make it simple , you can think about dependency injection as the responsibility of the class or struct. What do I mean by that? Ok let me explain based on example.

struct Properties {
     var weight = 40
     var tall = 170
     var hairColor = black
}

class People {
     var properties : Properties?
}

In above example , We have 1 class and 1 struct which are:
1. Perperties : This struct is storing all the human property.
2. People : This class has a properties object which is type Properties.

To instantailize the properties object we can do it in two ways which are
- Without Dependency Injection
- With Dependency Injection

Without Dependency Injection

struct Properties {
     var weight = 40
     var tall = 170
     var hairColor = black
}

class People {
     var properties : Properties = Properties()
}

As you can see in above example. The properties object has been instantailize by the People class itself. This mean that The People class is not only known the information about Properties struct , it also know and responsible about its Instantailize.

With Dependency Injection

struct Properties {
     var weight = 40
     var tall = 170
     var hairColor = black
}

class People {
     var properties : Properties?

     init(properties : Properties){
         self.properties = properties
     }
}

With Dependency Injection as you can see , The properties object is instantailize though the init(). So it mean the People class , Doesn't know bother about its instantialize.

2. Type of Dependency Injection

The type of dependency injection are vary , Some say 4 and some say 3 but today we are going to discuss the 3 type.

Initailizer Injection

The above example that we use is Initailizer Injection. The benefit of this initailizer injection is that the dependency that pass during injection can be made immutable.

struct Properties {
     var weight = 40
     var tall = 170
     var hairColor = black
}

class People {
     var properties : Properties?

     init(properties : Properties){
         self.properties = properties
     }
}

Properties Injection

Dependencies can also be injected by declaring an internal or public property on the class or structure that requires the dependency but this type of injection the dependency is not immutable.

struct Properties {
     var weight = 40
     var tall = 170
     var hairColor = black
}

class People {
     var properties : Properties?

     override func viewDidLoad() {
        super.viewDidLoad()
        let viewController = ViewController()
        viewController.properties = Properties()
    }
}

Method Injection

You also can inject dependencies through method.This is easy to do by declaring a method that accepts the dependency as an argument.

struct Properties {
     var weight = 40
     var tall = 170
     var hairColor = black
}

class People {
     func (with properties: Properties) {
           // Do something
     } 
}

3. Why do We Use Dependency Injection?

There are many reason but the number one reason why we use Dependency Injection is :

1. Testability

With dependency injection Unit testing is much easier with dependency injection. Dependency injection allows developers to replace an object's dependencies with mock objects, which makes isolating behavior and setting up unit tests easier and less complicated.

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1