DIcontainer의 도입이유

스크린샷 2024-11-13 오후 6.01.21.png

DIContainer

public typealias DependencyContainerClosure = (DIContainable) -> Any

public protocol DIContainable {
  func register<T>(type: T.Type, containerClosure: @escaping DependencyContainerClosure)
  func resolve<T>(type: T.Type) -> T?
}

해당 프로토콜을 채택한 DIContainer을 보면

public final class DIContainer: DIContainable {
  public static let shared: DIContainer = DIContainer()
  
  var services: [String: DependencyContainerClosure] = [:]
  
  private init() {}
  
  public func register<T>(type: T.Type, containerClosure: @escaping DependencyContainerClosure) {
    services["\\(type)"] = containerClosure
  }
  
  public func resolve<T>(type: T.Type) -> T? {
    let service = services["\\(type)"]?(self) as? T
    
    if service == nil {
      Logger.log(message: "\\(#file) - \\(#line): \\(#function) - \\(type) resolve error")
    }
    
    return service
  }
}