2

While trying and playing around with Typhoon DI, I realized that LazySingleton scope is not working as expected meaning lazy properties are injected even before they are used. Being more concrete I created a TyphoonAssembly as follow :

public class AppAssembly : TyphoonAssembly {

    public dynamic func knight() -> AnyObject{

        return TyphoonDefinition.withClass(Knight.self){
            (definition) in

            definition.injectProperty("name", with: "Dragon")

            definition.injectProperty("horse")

            definition.scope = TyphoonScope.LazySingleton

        }
    }

    public dynamic func horse() -> AnyObject{

        return TyphoonDefinition.withClass(Horse.self){
            (definition) in

            definition.injectProperty("color", with: "red")

            definition.scope = TyphoonScope.LazySingleton
        }
    }

}

where Knight is NSObject and has validateProperties function

class Knight:NSObject {

    var name:String?
    var horse: Horse?

    func validateProperties(){

        if name != nil{

            println("Name not nil")
        }

        if horse != nil{

            println("Horse not nil")
        }
    }
}

After activating assembly and getting knight from it, calling validateProperties function always print Name not nil and Horse not nil even these properties are never used in my code. Am I missing something here or simply lazy injection does not work same as Swift lazy stored properties do?

4

1 回答 1

2

您对惰性单例一词的解释是有道理的,但它不是正确的。TyphoonScopeLazySingleton意味着 Typhoon 在请求之前不会实例化整个对象。一旦要求所有属性将被注入。没有按需注入的代理——如果你对这样的功能感兴趣,你介意在 Github 上向我们提出问题吗?

你是对的,如果类扩展并且属性是与 Objective-C 兼容的类型,那么这样的特性只能在 Swift 中工作NSObject,因为“纯”Swift 使用 C++ 风格的静态调度,因此运行时方法拦截/代理是不可能的。

这是Typhoon 示波器的用户指南

于 2015-07-10T14:36:00.973 回答