SDObjection is modified version of Objection - a lightweight dependency injection framework for Objective-C for MacOS X and iOS. For those of you that have used Guice objection will feel familiar. Objection was built to stay out of your way and alleviate the need to maintain a large XML container or manually construct objects.
- "Annotation" Based Dependency Injection
- Seamless support for integrating custom and external dependencies
- Custom Object Providers
- Meta Class Bindings
- Protocol Bindings
- Instance Bindings
- Lazily instantiates dependencies
- Eager Singletons
- Initializer Support
- Default and custom arguments
What's different from Objection
- No global context
- No objection_register or objection_register_singleton
- This is handled automatically in modules
- Injector lets you add and remove modules at any time
- Removing modules also remove their bindings
- Injector lets you inject into existing objects
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
[injector addModule:[[SomeModule alloc] init]];
[injector addModule:[[SomeOtherModule alloc] init] withName:@"otherModule"];
[injector removeModuleInstance:someModule];
[injector removeModuleClass:[SomeModule class]];
[injector removeModuleWithName:@"otherModule"];
[injector removeAllModules];
BOOL hasModule = [injector hasModuleClass:[SomeModule class]];
BOOL hasModuleWithName = [injector hasModuleWithName:name];
- (void)configure {
[super configure];
// Configure here
[self bindClass:[Foo class] toProtocol:@protocol(FooProtocol) asSingleton:YES];
[self bind:[[Bar alloc] init] toProtocol:@protocol(BarProtocol)];
[self registerSingleton:[SingleFoo class]];
[self registerEagerSingleton:[EagerBar class]];
// there are many more binding options...
}
- (void)start {
[super start];
// All configured bindings are ready to use
[[self.injector getObject:@protocol(BarProtocol)] bar];
[[self.injector getObject:[SingleFoo class]] foo];
}
- (void)unload {
// Optional unloading here. Gets called, when modules get removed.
// All configured bindings will get removed automatically.
[super unload];
}
You find the source files you need in Pods/Objection/Source
To use SDObjection with ARC, you might want to set it up with CocoaPods like this:
Create a Podfile and put it into your root folder of your project
platform :ios, '5.0'
pod 'SDObjection'
Setup CocoaPods, if not done already
$ sudo gem install cocoapods
$ pod setup
$ pod repo add sschmid-cocoapods-specs https://github.com/sschmid/cocoapods-specs
$ cd path/to/project
$ pod install
If you enjoy using SDObjection in your projects let me know, and I'll mention your projects here.