Search code examples
iosswiftdependency-injectionswinject

How to proper inject dependency using swinject


I'm trying to inject dependency using Swinject, and I have no clue what I'm doing wrong.

I have protocol, that handles registring user.

protocol AuthServiceProtocol {
func registerUser(email: String, password: String, completion: @escaping CompletionHandler) }

and a class that conforms to this protocol make all the logic:

class AuthService: AuthServiceProtocol {
func registerUser(email: String, password: String, completion: @escaping CompletionHandler) {

        let lowerCaseMail = email.lowercased()
        let body: [String: Any] = [
            "email": lowerCaseMail,
            "password" : password
        ]

        Alamofire.request(URL_REGISTER, method: .post, parameters: body, encoding: JSONEncoding.default, headers: HEADER).responseString { (response) in
            if response.result.error == nil {
                completion(true)
            } else {
                completion(false)
                debugPrint(response.result.error as Any)
            }
        }
    }
} 

so, in AppDelegate we register container and it looks like:

let container = Container() { container in
    container.register(AuthServiceProtocol.self) { _ in AuthService() }.inObjectScope(.container)
    container.register(CreateAccountVC.self) { r in
        let controller = CreateAccountVC()
        controller.authService = r.resolve(AuthServiceProtocol.self)
        return controller
    }
}

but in CreateAccountVC authService is empty. Any ideas how can i do it? CreateAccountVC is a subclass of ViewController, i have try'ed it by property, and constructors, but it's nil all the time.


Solution

  • Check your code:

    var container : Container {
            let container = Container()
            container.register(AuthServiceProtocol.self) { _ in AuthService() }.inObjectScope(.container)
            container.register(CreateAccountVC.self) { r in
                let controller = CreateAccountVC()
                controller.authService = r.resolve(AuthServiceProtocol.self)
                print(r.resolve(AuthServiceProtocol.self))
                return controller
            }
    
            return container
        }
    

    You have computed property and every time you call it, it creates a NEW Container object.

    Refactor your code to have a single Container and I believe you will be good to go.


    EDIT:

    Here's a working code snippet. Below is a small wrapper class to abstract concrete DI service (in case Swinject is one day replace by something else):

    import Swinject
    
    public class ConfigurationProvider {
    
        // Currently using Swinject
        private let backingService = Container()
    
        // Singleton
        public static let shared = ConfigurationProvider()
    
        // Hidden initializer
        private init() {}
    
    
        // MARK: - Bind / Resolve
    
        public func bind<T>(interface: T.Type, to assembly: T) {
            backingService.register(interface) { _ in assembly }
        }
    
        public func resolve<T>(interface: T.Type) -> T! {
            return backingService.resolve(interface)
        }
    }
    
    
    // Extension methods to ignore 'shared.' call, like:
    // ConfigurationProvider.bind(interface: IAssembly, to: Assembly())
    // ConfigurationProvider.resolve(interface: IAssembly)
    
    public extension ConfigurationProvider {
    
        static func bind<T>(interface: T.Type, to assembly: T) {
            ConfigurationProvider.shared.bind(interface: interface, to: assembly)
        }
    
        static func resolve<T>(interface: T.Type) -> T! {
            return ConfigurationProvider.shared.resolve(interface: interface)
        }
    }
    

    Usage:

    class RSAuthLoginModuleAssembly: IAuthLoginModuleAssembly {
    
    }
    
    // Register:
    
    ConfigurationProvider.bind(interface: IAuthLoginModuleAssembly.self, to: ConcreteAuthLoginModuleAssembly())
    
    // Resolve:
    
        guard let assembly = ConfigurationProvider.resolve(interface: IAuthLoginModuleAssembly.self) else {
            throw NSError(domain: "Assembly cannot be nil", code: 999, userInfo: nil)
        }