-
Notifications
You must be signed in to change notification settings - Fork 269
Backing a TyphoonAssembly with a factory protocol
It's possible to define a protocol to back your assemblies, for all or some assembly methods, with or without runtime arguments. For example, instead of the following:
// FriendListViewController.h
@interface FriendListViewController
@property (nonatomic, strong, readonly) MyAssembly* assembly;
@end
// MyAssembly.h
@interface MyAssembly: TyphoonAssembly
- (id)userDetailsControllerForUser:(User *)user;
@end
We can extract MyAssembly
's interface into a protocol and define this instead:
// UserDetailsControllerFactoryProtocol.h
@protcol UserDetailsControllerFactoryProtocol
- (id)userDetailsControllerForUser:(User *)user;
@end
// FriendListViewController.h
@interface FriendListViewController
@property (nonatomic, strong, readonly) id<UserDetailsControllerFactoryProtocol> assembly;
@end
... then we make MyAssembly
conform to this assembly protocol. It can then be injected into FriendListViewController
as follows:
@interface MyAssembly : TyphoonAssembly <UserDetailsControllerFactoryProtocol>
// No declarations here necessary
@end
@implementation MyAssembly
- (id)friendListController
{
return [TyphoonDefinition withClass:[FriendListViewController class]
configuration:^(TyphoonDefinition *definition) {
[definition injectProperty:@selector(assembly) with:self];
}];
}
- (id)userDetailsControllerForUser:(User *)user
{
return [TyphoonDefinition withClass:[UserDetailsViewController class]
configuration:^(TyphoonDefinition *definition) {
[definition useInitializer:@selector(initWithPhotoService:user)
parameters:^(TyphoonMethod *initializer) {
[initializer injectParameterWith:[self photoService];
[initializer injectParameterWith:user];
}];
}];
}
@end
### Using a Backed
It's possible to define a protocol to back your assemblies, for all or some assembly methods, with or without runtime arguments. For example, instead of the following:
// FriendListViewController.h
@interface FriendListViewController
@property (nonatomic, strong, readonly) MyAssembly* assembly;
@end
// MyAssembly.h
@interface MyAssembly: TyphoonAssembly
- (id)userDetailsControllerForUser:(User *)user;
@end
We can extract MyAssembly
's interface into a protocol and define this instead:
// UserDetailsControllerFactoryProtocol.h
@protcol UserDetailsControllerFactoryProtocol
- (id)userDetailsControllerForUser:(User *)user;
@end
// FriendListViewController.h
@interface FriendListViewController
@property (nonatomic, strong, readonly) id<UserDetailsControllerFactoryProtocol> assembly;
@end
... then we make MyAssembly
conform to this assembly protocol. It can then be injected into FriendListViewController
as follows:
@interface MyAssembly : TyphoonAssembly <UserDetailsControllerFactoryProtocol>
// No declarations here necessary
@end
@implementation MyAssembly
- (id)friendListController
{
return [TyphoonDefinition withClass:[FriendListViewController class]
configuration:^(TyphoonDefinition *definition) {
[definition injectProperty:@selector(assembly) with:self];
}];
}
- (id)userDetailsControllerForUser:(User *)user
{
return [TyphoonDefinition withClass:[UserDetailsViewController class]
configuration:^(TyphoonDefinition *definition) {
[definition useInitializer:@selector(initWithPhotoService:user)
parameters:^(TyphoonMethod *initializer) {
[initializer injectParameterWith:[self photoService];
[initializer injectParameterWith:user];
}];
}];
}
@end
You'll need to use TyphoonAssembly <FactoryProtocol>
syntax. See How do I make one assembly use components from another?.
Tip: Backing your factory methods with a protocol means that, if you inject the assembly your classes do not have a direct dependency on Typhoon. This is considered to be good practice, and in the spirit of dependency injection.
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.