Search code examples
objective-ctyphoon

Typhoon support "Autowire" and "Scope" definition


If I compare Typhoon with one of the common IOC container spring in java i could not find two important freatures in the documentation.

How to annotate @autowired? How to annotate @Scope? Especially distinglish between SCOPE_SINGLETON and SCOPE_PROTOTYPE.

More about spring here: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/html/beans.html#beans-standard-annotations


Solution

  • Typhoon supports the prototype and singleton scopes along with two other scopes designed specifically for mobile and desktop applications.

    In a server-side application, the server may be supporting any of the application's use-cases at a given time. Therefore it makes sense for those components to have the singleton scope. In a mobile application, while there are background services its more common to service one use case at a time. And there are memory, CPU and batter constraints.

    Therefore the default scope with Typhoon is TyphoonScopeObjectGraph, which means that references to other components while resolving eg a top-level controller will be shared. In this way an object graph can be loaded up and then disposed of when done.

    There's also the following:

    Auto-wiring macros vs native style assembly:

    Unfortunately, Objective-C has only limited run-time support for "annotations" using macros. So the option was to use either a compile-time pre-processor, which has some drawbacks, or to work around the limitations and force it in using a quirky style. We decided that its best (for now) to use Macros only for simple convention-over-configuration cases.

    For more control we strongly recommend using the native style of assembly. This allows the following:

    • Modularize an application's configuration, so that the architecture tells a story.
    • IDE code-completion and refactoring works without any additional plugins.
    • Components can be resolved at runtime using the assembly interface, using Objective-C's AOP-like dynamism.

    To set the scope using the native style:

    - (id)rootController
    {
        return [TyphoonDefinition withClass:[RootViewController class] 
            configuration:^(TyphoonDefinition* definition)
        {
            definition.scope = TyphoonScopeSingleton;
        }];
    }