Dependency Injection Simply

Omar Saibaa
3 min readAug 23, 2023

--

Dependency injection (DI) is a design pattern that allows us to separate the dependencies of a class from the class itself. This makes it easier to test the class and to make changes to its dependencies.

benefits of dependency injection?

1- Testability: Dependency injection makes it easier to test code because the dependencies can be mocked or stubbed out. This means that the code can be tested without having to worry about the actual dependencies.

2- Flexibility: Dependency injection makes it easier to change dependencies. This is because the dependencies are not hard-coded into the code, but are instead passed in as arguments. This makes it easier to change the dependencies without having to change the code.

3- Maintainability: Dependency injection makes code more maintainable because it separates the code from the dependencies. This makes it easier to understand the code and to make changes to it.

4- Cohesion: Code that uses dependency injection can be more cohesive because the dependencies are related to the class’s purpose. This makes the code easier to understand and maintain.

How to implement dependency injection in swift?

Constructor injection:

it is the most common way to inject dependencies in Swift. In constructor injection, the dependencies are passed to the class’s initializer. For example:

Constructor injection

In this example, the NetworkManager dependency is injected into the Product class through the constructor.

Setter injection:

it is a way to inject dependencies into a class’s properties. In setter injection, the dependencies are set on the properties of the class after the class has been initialized. For example:

Setter injection

In this example, the NetworkManager dependency is injected into the Product class's networkManager property through the setNetworkManager method.

Property injection:

it is is a way to inject dependencies into a class’s properties using property wrappers. Property wrappers are a way to abstract the implementation of a property from its declaration. For example:

Property injection

In this example, the name, price, and networkManager properties are all injected using the Inject property wrapper. This is a newer way to inject dependencies in Swift, and it can be useful if you want to make the dependency injection more explicit.

advantages and disadvantages of each of the three last types of dependency injection:

Constructor injection:

Advantages:

  • It is the most explicit way to inject dependencies.
  • It is the easiest way to test dependencies.
  • It is the most flexible way to change dependencies.

Disadvantages:

  • It can make the constructor more complex.
  • It can make the class less reusable.

Setter injection:

Advantages:

  • It is less intrusive than constructor injection.
  • It can make the class more reusable.

Disadvantages:

  • It is less explicit than constructor injection.
  • It can be more difficult to test dependencies.

Property injection:

Advantages:

  • It is the most concise way to inject dependencies.
  • It can be used with property wrappers.

Disadvantages:

  • It is the least explicit way to inject dependencies.
  • It can be more difficult to test dependencies.

How to choose one of them?

the best way depends on the specific situation.
If you need to make the dependency injection explicit and easy to test, then constructor injection is the best choice.
If you need to make the class more reusable, then setter injection is a good option.
If you need to make the dependency injection concise, then property injection is a good choice.

I hope this helps you to understand .. Don’t forget to follow me ,peace.

--

--