-
Notifications
You must be signed in to change notification settings - Fork 269
Auto injection (Objective C)
Typhoon is capable of performing [all kinds of injections](types of injections). This power and flexibility is invariably needed at some point. Often, however, all that's required is simple property injection. In these cases, auto-wiring can save time.
Use Typhoon auto-injection to:
- Quickly perform property injection of classes or protocols, matching by type.
- Avoid having to register a definition in an assembly (this is nice with Storyboards)
Bootstrap your Typhoon-powered application using plist integration, refer to the Typhoon Sample Application for an example.
//This is also included in the umbrella Typhoon.h
#import "TyphoonAutoInjection.h"
@interface CollectOffersController : UIViewController
@property(nonatomic, strong) InjectedProtocol(ImageProvider) imageProvider;
@end
The CollectOffersController
will be injected with an instance defined in the assembly that matches the protocol id<ImageProvider>
.
Note that we do not include the id type or brackets, (ie id <ImageProvider>
) in the protocol name. This is implied.
@interface ChannelBrowserController : UIViewController
@property (nonatomic, strong) InjectedClass(HttpWebService) webService;
@end
The ChannelBrowserController
above will be injected with an instance defined in the assembly that matches is a kind of HttpWebService
.
Note that we do not include '*' character (ie HttpWebService *service). This is implied.
- When you'd like to save time.
- When you'd like to document a classes dependency information in its header file.
- When you wish to avoid coupling your class to any Typhoon APIs. Define injections in a
TyphoonAssembly
instead. - When you wish to document an applications assembly in a centralized fashion.
- Wire infrastructure definitions explicitly to document application assembly.
- Use auto-injection for top-level components, eg view controllers.
- Use auto-injection for test-cases (see below).
Auto-injection can be used to perform integration testing with test frameworks. Here's an example with XCTest
@interface TestResultDaoTests : XCTestCase
@property (nonatomic, strong) InjectedProtocol(TestResultDao)testResultDao;
@end
Override setup method:
@implementation TestResultDaoTests
- (void)setUp
{
[[[ApplicationAssembly new] activate] inject:self];
}
And now perform tests:
- (void)test_createOrUpdate
{
//Do something with class under tests
}
By Modularizing Assemblies you can override certain components to put your system int the required stage for integration testing.
See also: Integration Testing
NB: If you wish you can mix auto-wiring and along with a definition in the assembly. All of the auto-wiring injections will be applied along with ones defined in the assembly definition. When duplication occurs the manual rules defined in the assembly win.
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.