简体   繁体   中英

Auto Injection with typhoon + swift

I'm using typhoon in a swift project, as far as i understand i have to map all injections explicitly in a TyphoonAssembly like this:

 public dynamic func appDelegate() -> AnyObject {
    return TyphoonDefinition.withClass(AppDelegate.self) {
        (definition) in

        definition.injectProperty("cityDao", with:self.coreComponents.cityDao())
        definition.injectProperty("rootViewController", with:self.rootViewController())
    }
}

this seems hard to manage, and very fragile (when refactoring).

I see that there is support for auto injection (matching by types) here: https://github.com/appsquickly/Typhoon/wiki/Auto-injection-(Objective-C) but this is for objetive c.

Does anyone know of a way i can wire up the injection without explicitly registering props with their name as string?

Thanks!

(Typhoon creator here).

Typhoon is a dynamic, introspective dependency injection container , and uses the Objective-C run-time. There are the following limitations, when it comes to Swift:

  • With Objective-C it avoids magic strings, allowing the use of ordinary IDE refactoring tools, however in Swift selectors are magic strings.
  • The Objective-C runtime only provides type information for properties, not method or initializer parameters. So only properties can support auto-wiring of any kind (macros, implicit, etc).
  • There is no annotation or macro system for Swift, (but it does have 1st class functions).

You can instruct Typhoon to auto-wire a property in Swift using the following:

 public dynamic func appDelegate() -> AnyObject {
    return TyphoonDefinition.withClass(AppDelegate.self) {
        (definition) in

        definition.injectProperty("cityDao")
        definition.injectProperty("rootViewController")
    }
}

. . and this will match by type, just as the auto-wiring Objective-C macros do. However this does not avoid specifying the name of the property to be injected. There is no other way to do this in Swift. :(

Its worth mentioning there are additional limitations when using Typhoon with Swift:

  • "Pure" Swift classes - not extending a Cocoa base class or declaring the @objc directive - don't support introspection, dynamic dispatch or dynamic method invocation. Typhoon only works with Cocoa classes.
  • Swift protocols require the @objc directive.

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