Skip to content

Auto injection (Objective C)

Jasper Blues edited this page Aug 9, 2017 · 10 revisions

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

Bootstrap your Typhoon-powered application using plist integration, refer to the Typhoon Sample Application for an example.

Import the TyphoonAutoInjection header (or Typhoon.h)

//This is also included in the umbrella Typhoon.h
#import "TyphoonAutoInjection.h" 

Auto-injecting Protocols

@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.

Auto-injecting Classes

@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.

Use Auto-injection . . .

  • When you'd like to save time.
  • When you'd like to document a classes dependency information in its header file.

Don't use Auto-injection . .

  • 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.

Recommendation

  • 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).


Using Auto-injection with Test Frameworks

Auto-injection can be used to perform integration testing with test frameworks. Here's an example with XCTest

Declare properties to be injected

@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.