Dependency Injection - Swift


#<b> Introduction</b>

Today topic that we are going to cover is , Dependency Injection and we are going to discuss about 
<b> 1. What is dependency injection? 
2. Type of dependency injection?
3. Why do we use dependency injection? </b>
ok. Now let's get started.

##<b>1. What Is Dependency Injection?</b>

Base on James Shore definition <b><i>Dependency injection means giving an object its instance variables. Really. That's it.</b></i> 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. <b>What do I mean by that? </b> Ok let me explain based on example.

```swift
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. <b> Perperties </b> : This struct is storing all the human property.
2. <b> People </b> : 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


###<b>Without Dependency Injection</b>
```swift
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.

###<b>With Dependency Injection</b>
```swift
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. 

##<b>2. Type of Dependency Injection</b>

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

####<b>Initailizer Injection</b>

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. 

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

class People {
     var properties : Properties?

     init(properties : Properties){
         self.properties = properties
     }
}
```
####<b>Properties Injection</b>
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.

```swift
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()
    }
}
```
####<b>Method Injection</b>
You also can inject dependencies through method.This is easy to do by declaring a method that accepts the dependency as an argument.

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

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

##<b>3. Why do We Use Dependency Injection?</b>

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

<b> 1. Testability </b>

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.