From c21a8efa5f767979b419c943363460ec7f214639 Mon Sep 17 00:00:00 2001 From: Jungpyo Hong Date: Wed, 23 Jun 2021 05:52:47 -0500 Subject: [PATCH] update DI --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index e25fa2f..0ff4d98 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,54 @@ weak var delegate: SomeProtocol? ``` ## Dependency Injection +Dependency injection is a pattern that can be used to eliminate the need for singletons in a project + + + 1. Raise Transparency + 2. Improve Testability + +### Type of Dependency Injection + + 1. initializer injection + + ``` swift + class DataManager { + + private let serializer: Serializer + + init(serializer: Serializer) { + self.serializer = serializer + } + +} + ``` + + 2. property injection + + ```swift + import UIKit + +class ViewController: UIViewController { + + var requestManager: RequestManager? + +} +``` + + 3. method injection + + ```swift + import Foundation + +class DataManager { + + func serializeRequest(request: Request, withSerializer serializer: Serializer) -> Data? { + ... + } + +} +``` + [Nuts and Bolts of Dependency Injection in Swift](https://cocoacasts.com/nuts-and-bolts-of-dependency-injection-in-swift)