Skip to content
Jasper Blues edited this page May 12, 2017 · 30 revisions

Typhoon provides the following scopes:

TyphoonScopeObjectGraph (default)

This scope means that when a TyphoonDefinition is assembled, any dependencies will be treated as shared instances during assembly. Once resolution is complete they are not retained by Typhoon. This allows instantiating an entire object graph, for a use-case (say for a ViewController), and then discarding it when that use-case has completed.

Example: Circular dependencies

Here we'll wire up a delegate:

- (JobExecutionViewController *)jobExecutionViewController
{
    return [TyphoonDefinition withClass:[JobExecutionViewController class]
        configuration:^(TyphoonDefinition *definition) {

            [definition injectProperty:@selector(view) 
               with:[self jobExecutionView]];
        }];
}

- (JobExecutionView *)jobExecutionView
{
    return [TyphoonDefinition withClass:[JobExecutionView class] 
        configuration:^(TyphoonDefinition *definition) {

        [definition injectProperty:@selector(delegate) 
            with:[self jobExecutionViewController]];
    }];
}

The view controller that gets injected into the view's delegate property as a circular dependency will be the same instance that was declared above. This also applies with complex object graphs, as well as . . .

With runtime arguments:

- (JobExecutionViewController *)jobExecutionViewControllerWithJob:(Job *)job
{
    return [TyphoonDefinition withClass:[JobExecutionViewController class]
        configuration:^(TyphoonDefinition *definition) {

            [definition useInitializer:@selector(initWithJob:) 
                parameters:^(TyphoonMethod *initializer) {

                [initializer injectParameterWith:job];
            }];

            [definition injectProperty:@selector(view) 
               with:[self jobExecutionViewWithJob:job]];
        }];
}

- (JobExecutionView *)jobExecutionViewWithJob:(Job *)job
{
    return [TyphoonDefinition withClass:[JobExecutionView class] 
        configuration:^(TyphoonDefinition *definition) {

        [definition injectProperty:@selector(delegate) 
            with:[self jobExecutionViewControllerWithJob:job]];
    }];
}

Again the view controller that is injected into the view's delegate property will be the same instance that was declared above.

TyphoonScopePrototype

Unlike TyphoonScopeObjectGraph, indicates that a new instance should always be created by Typhoon, whenever specified as a dependency to another definition.

TyphoonScopeSingleton

Indicates that Typhoon should retain the instance effectively creating a singleton, at least as long as the TyphoonComponentFactory that holds it remains.

TyphoonScopeLazySingleton

This scope behaves the same as TyphoonScopeSingleton, but the object is not created unless or until it is needed.

TyphoonScopeWeakSingleton

Indicates that a shared instance should be created as long as necessary. When your application's classes stop referencing this component it will be deallocated until needed again.

Setting Scope:

- (RootViewController *)rootController
{
    return [TyphoonDefinition withClass:[RootViewController class] 
    configuration:^(TyphoonDefinition* definition) {

        definition.scope = TyphoonScopeSingleton;
    }];
}