如果我将依赖注入容器用于 1 个以上的依赖项,则返回 nil

Dependency Injection Container returns nil if I use it for more than 1 dependency

提问人:Alon Shlider 提问时间:3/1/2023 更新时间:3/1/2023 访问量:62

问:

我尝试实现自己的依赖注入容器,这是代码 -

import Foundation

class Container {
    
    static let shared = Container()
    var factoryDictionary : [String : () -> Any] = [:]
    
    init (){
        register(type: BasicApplicationViewModel.self, {
            BasicApplicationViewModel()
        })
        register(type: BasicApplicationNetworkService.self, {
            BasicApplicationNetworkService()
        })
        register(type: BasicApplicationView.self, {
            BasicApplicationView()
        })
    }
    
    func register<Service>(type: Service.Type, _ factory: @escaping () -> Service) {
        factoryDictionary[String(describing: type.self)] = factory
    }
    
    func resolve<Service>(_ type: Service.Type) -> Service? {
        return factoryDictionary[String(describing: type.self)]?() as? Service
    }
    
}


@propertyWrapper
struct InjectManual<Type> {
    var type : Type
    
    init (){
        self.type = Container.shared.resolve(Type.self)!
    }
    
    var wrappedValue: Type {
        get {
            return self.type
        }
        mutating set {
            self.type = newValue
        }
    }
}

我正在我的 SwiftUI 应用程序中的 3 个不同位置使用它 -

import Foundation
import Combine
import SwiftUI


@main
struct BasicApplicationApp: App {
    
    @InjectManual
    var statingView : BasicApplicationView
    
    var body: some Scene {

        WindowGroup {
            statingView
        }
    }
}

struct BasicApplicationView: View {
    
    
    @InjectManual
    @ObservedObject var viewmodel : BasicApplicationViewModel
    
    var body: some View {
        
        switch(viewmodel.uiState.state) {
            case.initial: EmptyView()
                
            case.data:
                ViewDataState(userResponse: viewmodel.uiState.userResponse)
                
            case.error: EmptyView()
        }
    }
}


class BasicApplicationViewModel: ObservableObject {
    
    @InjectManual
    var networkService : BasicApplicationNetworkService
        
    @Published var uiState = UiState()
    
    var cancellable : AnyCancellable?
    
    init(){
        fetchUsers()
    }
    
    func fetchUsers() {
        cancellable = networkService.fetchUsers().sink(receiveCompletion: { _ in }, receiveValue: { userResult in
            self.uiState.resetValues(state: UiState.State.data, userResponse: userResult)
        })
    }
    
    struct UiState{
        
        var state : State = State.initial
        var userResponse = UserResponse()
        var errorMessage = ""
        
        
        enum State {
            case data
            case error
            case initial
        }
        
        mutating func resetValues(
            state: State = State.initial,
            userResponse: UserResponse = UserResponse(),
            errorMessage : String = "") {
            self = UiState(state: state, userResponse: userResponse,errorMessage: errorMessage)
        }
    }
}

我面临的问题是,当尝试使用我的属性包装器超过 1 次时,无论它在哪里(无论是在注入网络服务的 VM、注入 VM 的视图还是在注入视图的应用程序中),我都会在解析方法上得到 nil。我可以在整个应用程序中使用一次注入,但是当我开始多次使用它时,我得到了一个零崩溃,我不知道为什么。InjectManual

swift swiftui mvvm 依赖注入

评论


答: 暂无答案