Use Typhoon to inject property into class that is not initialised by Typhoon Assembly

132 Views Asked by At

I am working on a private pod and currently I have some difficulty using Typhoon to inject property into the bootstrap class.

My class:

public class MyLibrary: NSObject {
    var dependency: MyDependencyProtocol?
}

My assembly:

open class MyLibraryAssembly: TyphoonAssembly {

    open dynamic func lib() -> Any
    {
        return TyphoonDefinition.withClass(MyLibrary.self) { (definition) in
            definition?.injectProperty(#selector(getter: MyLibrary.dependency), with: self.dependency())
            definition?.scope = .singleton
        }
    }
}

However, the consumer app will use var lib = MyLibrary() to initialise the object rather than using dependency injection. How do I inject dependency into the MyLibrary class when the init is called?

1

There are 1 best solutions below

0
Jasper Blues On

It might be better to define a class method in your MyLibrary class to return a build instance of your MyLibrary class. This method would:

  • Bootstrap Typhoon
  • Ask Typhoon for a built & configured instance of your library
  • Return it to the method caller

Example: MyLibrary.instance() or MyLibrary.instanceWithConfig(someConfig).

  • Callers of these methods won't care how the object is build behind behind the scenes - just trust that a built & configured instance is returned.
  • Still the option to assembly an instance of the library manually using the public constructor.