简体   繁体   中英

Using Typhoon, create instance of a subclass of a class which has dependencies

I am using Typhoon for dependencies injection in Swift for iOS.

I have created an assembly to inject dependencies in a class called BaseRequest like this:

public class NetworkAssembly: TyphoonAssembly {
    public dynamic func baseRequest() -> AnyObject {
        return TyphoonDefinition.withClass(BaseRequest.self){
            (definition) in
            definition.useInitializer("initWithRetryCount:userUmbrella:networkQueueManager:"){
                (initializer) in

                initializer.injectParameterWith((TyphoonConfig("network.request.retry.count") as! NSNumber).integerValue)
                initializer.injectParameterWith(self.coreComponents.userUmbrella())
                initializer.injectParameterWith(self.networkQueueManager())

            }
        }
    }
}

Now, I am trying to create a subclass of BaseRequest with a factory method like this:

class DownloadLibrariesRequest: BaseRequest {
    var libraries:Array<String> = []

    class func downloadLibraries(libraries:Array<String>)->Void{
        let request: DownloadLibrariesRequest = DownloadLibrariesRequest(.....)
        request.libraries = libraries
    }
}

I need to be able to create an instance of DownloadLibrariesRequest and call the NetworkAssembly for BaseRequest since I need to use another init in the subclasses. Also, I need to mention that I will have around 50 such subclasses, so creating assemblies for all of them doesn't sound too great at the moment.

要创建具有共享配置的定义,为避免重复,请使用功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM