-
Notifications
You must be signed in to change notification settings - Fork 268
Scopes
Typhoon provides the following scopes:
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.
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 . . .
- (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.
Unlike TyphoonScopeObjectGraph
, indicates that a new instance should always be created by Typhoon, whenever specified as a dependency to another definition.
Indicates that Typhoon should retain the instance effectively creating a singleton, at least as long as the TyphoonComponentFactory
that holds it remains.
This scope behaves the same as TyphoonScopeSingleton, but the object is not created unless or until it is needed.
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.
- (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.