Skip to content
jasperblues edited this page Nov 8, 2014 · 30 revisions

[Types of Injections](Types of Injections) | [What can be Injected](What can be Injected) | Modules | Scopes | Obtaining Built Components | [Integration Testing](Integration Testing)

Typhoon provides the following scopes:

##TyphoonScopeObjectGraph (default)

This scope is essential (and unique to Typhoon) for mobile and desktop applications. When a component is resolved, any dependencies with the object-graph scope will be treated as shared instances during resolution. Once resolution is complete they are not retained by the TyphoonComponentFactory. 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.

An example is shown below:

- (GiftDeliveryAddressController *)giftDeliveryControllerWithGift:(Gift *)gift 
    backgroundImage:(UIImage *)image

{
    return [TyphoonDefinition withClass:[GiftDeliveryAddressController class] 
        configuration:^(TyphoonDefinition *definition) {

        [definition useInitializer:@selector(initWithView:offerDao:client:) 
             parameters:^(TyphoonMethod *initializer) {

            [initializer injectParameterWith:[self giftDeliveryAddressView:gift backgroundImage:image]];
            [initializer injectParameterWith:[_persistenceComponents offerDao]];
            [initializer injectParameterWith:[_networkComponents httpClient]];
        }];
        [definition injectProperty:@selector(title) with:@"REDEEM"];
    }];
}

- (GiftDeliveryAddressView *)giftDeliveryAddressView:(Gift *)gift backgroundImage:(UIImage *)image
{
    return [TyphoonDefinition withClass:[GiftDeliveryAddressView class] 
        configuration:^(TyphoonDefinition *definition) {

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

            [initializer injectParameterWith:gift];
            [initializer injectParameterWith:image];
        }];

        //Components with object-graph scope refer to the same instance during resolution
        [definition injectProperty:@selector(delegate) 
            with:[self giftDeliveryControllerWithGift:gift backgroundImage:image]];
    }];
}

In the example above, the view's delegate property is a circular dependency back to the controller. Because the components have ObjectGraph scope, this refers to the same instance.

##TyphoonScopePrototype

Indicates that a new instance should always be created by Typhoon, whenever this component is obtained from an assembly or referenced by another component.

##TyphoonScopeSingleton

Indicates that Typhoon should retain the instance that exists for as long as the TyphoonComponentFactory exists.

##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 for a Component:

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