如何使用Typhoon将属性注入基类

时间:2016-05-16 07:53:49

标签: ios swift dependency-injection typhoon

我有类结构,如:

class BaseNetworkClient: NSObject {
    var defaultParametersBuilder: DefaultParametersBuilder!
}

class NetworkClient: BaseNetworkClient {

}

class DefaultParametersBuilder: NSObject {

}

和台风大会

class CoreComponentsAssembly: TyphoonAssembly {
    dynamic func baseNetworkClient() -> AnyObject {
        return TyphoonDefinition.withClass(BaseNetworkClient.self) {
            (definition) in
            definition.scope = .ObjectGraph
            definition.injectProperty(#selector(self.defaultParametersBuilder), with: self.defaultParametersBuilder())
        }
    }

    dynamic func networkClient() -> AnyObject {
        return TyphoonDefinition.withClass(NetworkClient.self) {
            (definition) in
            definition.scope = .ObjectGraph
        }
    }

    dynamic func defaultParametersBuilder() -> AnyObject {
        return TyphoonDefinition.withClass(DefaultParametersBuilder.self) {
            (definition) in
            definition.scope = .ObjectGraph
        }
    }
}

BaseNetworkClient不会向任何类注入,但NetworkClient会从中继承。问题是我无法向BaseNetworkClient注入任何属性。属性总是零。怎么了?

1 个答案:

答案 0 :(得分:2)

摘要和基本定义在项目wiki中进行了描述:https://github.com/appsquickly/Typhoon/wiki/Types-of-Injections#abstract-and-base-definitions

要继承注射,您需要为定义设置父级:

definition.parent = self.baseNetworkClient()

大会将改变如下:

dynamic func baseNetworkClient() -> AnyObject {
    return TyphoonDefinition.withClass(BaseNetworkClient.self) {
        (definition) in
        definition.injectProperty(#selector(self.defaultParametersBuilder), with: self.defaultParametersBuilder())
    }
}

dynamic func networkClient() -> AnyObject {
    return TyphoonDefinition.withClass(NetworkClient.self) {
        (definition) in
        definition.parent = self.baseNetworkClient()
        definition.scope = .ObjectGraph
    }
}