Skip to content

Auto injection (Objective C)

Jasper Blues edited this page Feb 15, 2015 · 10 revisions

With a strong foundation, 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

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.


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.