-
Notifications
You must be signed in to change notification settings - Fork 268
Scopes
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 would not be the case if the view controller declared above had TyphoonScopePrototype
. TyphoonScopeObjectGraph
rules continue to apply 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]];
}];
}
##TyphoonScopePrototype
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;
}];
}
Something still not clear? How about posting a question on StackOverflow.
Get started in two minutes.
Get familiar with Typhoon.
- Types of Injections
- What can be Injected
- Auto-injection (Objective-C)
- Scopes
- Storyboards
- TyphoonLoadedView
- Activating Assemblies
Become a Typhoon expert.
For contributors or curious folks.