{"id":16203316,"url":"https://github.com/sschmid/gummi-injection","last_synced_at":"2025-03-19T07:30:48.565Z","repository":{"id":6230718,"uuid":"7462312","full_name":"sschmid/Gummi-Injection","owner":"sschmid","description":"A lightweight dependency injection framework for Objective-C","archived":false,"fork":false,"pushed_at":"2013-03-28T17:34:46.000Z","size":561,"stargazers_count":9,"open_issues_count":1,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-28T16:18:14.791Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://sschmid.github.com/Gummi-Injection/","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sschmid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-01-06T01:21:47.000Z","updated_at":"2023-07-03T16:50:18.000Z","dependencies_parsed_at":"2022-09-13T10:21:17.388Z","dependency_job_id":null,"html_url":"https://github.com/sschmid/Gummi-Injection","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sschmid%2FGummi-Injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sschmid%2FGummi-Injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sschmid%2FGummi-Injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sschmid%2FGummi-Injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sschmid","download_url":"https://codeload.github.com/sschmid/Gummi-Injection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243976655,"owners_count":20377695,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T09:53:44.906Z","updated_at":"2025-03-19T07:30:48.116Z","avatar_url":"https://github.com/sschmid.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gummi Injection\n![Gummi Injection Logo](http://sschmid.com/Libs/Gummi-Injection/Gummi-Injection-128.png)\n\n## Description\nGummi Injection is a lightweight dependency injection framework for Objective-C.\n\n## Dependencies\nGummi-Injection uses [Gummi Reflection](https://github.com/sschmid/Gummi-Reflection) to inspect objects.\n\n## Features\n* Add and remove injection rules (mappings) at any time\n* One api for different kind of mappings ([injector map: to:])\n* Inject into existing objects\n* Extend injector context with modules\n* Add or remove modules at any time\n* Map blocks to classes or protocols\n* Map singletons and eager singletons\n* Handles circular dependencies for singletons\n* Injector can create unmapped dependencies, when they can be created like this [[MyObject alloc] init]\n* Specify a custom selector for objects to get notified when injection is complete\n* Child Injector\n\n## How to use Gummi Injection\n\n#### Get an injector\n\n```objective-c\n// Create your own injector\nGIInjector *injector = [[GIInjector alloc] init];\n\n// or use the shared injector\nGIInjector *injector = [GIInjector sharedInjector];\n\n// Child Injector\nGIInjector *childInjector = [injector createChildInjector];\n\n```\n\n#### Add injection rules [injector map:object to:keyObject]\n\n```objective-c\n// Map classes\n[injector map:[MyImplementation class] to:@protocol(MyProtocol)];\n\n// You don't have to do this. Gummi Injection will figure it out itself.\n[injector map:[MyImplementation class] to:[MyImplementation class]];\n\n// But you could do this\n[injector map:[MyImplementation1 class] to:[MyImplementation2 class]];\n\n// Map instances\n[injector map:car to:[Car class]]\n[injector map:car to:@protocol(Vehicle)]\n\n// Map singletons and eager singletons.\n// Eager singletons will be instantiated immediately\n[injector mapEagerSingleton:[Service class] to:@protocol(RemoteService)];\n[injector mapSingleton:[Model class] to:[Model class]];\n\n// Map blocks\nid (^factoryBlock)(GIInjector *) = ^(GIInjector *injector) {\n    id stuff = [injector getObject:[SomeStuff class]];\n    Car *car = [[Car alloc] initWithStuff:stuff];\n    return car;\n};\n[injector map:factoryBlock to:@protocol(Vehicle)];\n\n// You can remove mappings at any time\n[injector unMap:[Service class] from:@protocol(RemoteService)];\n```\n\n#### Mark properties for injection with \"inject\"\n\n```objective-c\n@class Wheel;\n\n@interface Car : NSObject \u003cVehicle\u003e\n@property(nonatomic, strong) Wheel *wheel1;\n@property(nonatomic, strong) Wheel *wheel2;\n@property(nonatomic, strong) Wheel *wheel3;\n@property(nonatomic, strong) Wheel *wheel4;\n@property(nonatomic) id \u003cEngine\u003e engine;\n@end\n\n@implementation Car\n\ninject(@\"wheel1\", @\"wheel2\", @\"wheel3\", @\"wheel4\", @\"engine\");\n\n// Optional selector gets performed, when injection is complete\ninjection_complete(@selector(startEngine))\n\n- (void)startEngine {\n    if (self.engine)\n        NSLog(@\"[%@] Brrrmmmmm....\", NSStringFromClass([self class]));\n}\n\n...\n@end\n```\n\n#### Create an object with all dependencies set\nWhen an object gets created by calling injector#getObject, all its dependencies will be satisfied as well.\n\n```objective-c\n// No need to set up rules for simple injections like Wheel\n// that can be created with alloc init.\n// For protocols there's no way to know which implementation to return -\n// we need to set up a rule for it.\n[injector map:[HybridEngine class] to:@protocol(Engine)];\n\nCar *car = [injector getObject:[Car class]];\n\n// or use protocols\n[injector map:[Car class] to:@protocol(Vehicle)];\nCar *car = [injector getObject:@protocol(Vehicle)];\n\n// or inject into existing objects\nCar *car = [[Car alloc] init];\n[injector injectIntoObject:car];\n\n// This will happen:\n// - getObject looks up type Car -\u003e no rule set -\u003e Instantiate Car and inject into object\n//     - Each Car wants Wheels\n//         - Look up type Wheel -\u003e no rule set -\u003e Instantiate Wheel and inject into object\n//     - Car wants \u003cEngine\u003e\n//         - Look up type \u003cEngine\u003e -\u003e rule found: [HybridEngine class]\n//         - Instantiate HybridEngine and inject into object\n// Done\n\n\n```\n\n## Modules\nModules are a wrapper for related mappings. They extend the context of the injector and can be added and removed at any time.\n\n```objective-c\nGIModule *module = [[GameModule alloc] init];\n[injector addModule:module];\n\n// After the game, remove the Module by class\n[injector removeModuleClass:[GameModule class]];\n// or by instance\n[injector removeModule:gameModule];\n```\n\n```objective-c\n@interface GameModule : GIModule\n@end\n\n@implementation GameModule\n\n- (void)configure:(GIInjector *)injector {\n    [super configure:injector];\n\n    [self mapSingleton:[MyGameModel class]\n                    to:@protocol(GameModel)];\n    \n    // Example Service starts automatically on init\n    [self mapEagerSingleton:[MyRemoteService class]\n                         to:@protocol(RemoteService)];\n}\n\n- (void)unload {\n    // For convenience, close all connections to stop service\n    Service *service = [_injector getObject:@protocol(RemoteService)];\n    [service close];\n\n    [super unload];\n}\n\n- (void)dealloc {\n    NSLog(@\"Service and Model get dealloced with me.\");\n}\n\n@end\n```\n\n## Install Gummi Injection\nYou find the source files you need in Gummi-Injection/Classes.\n\nYou also need:\n* [Gummi Reflection] (https://github.com/sschmid/Gummi-Reflection) - Reflection for Objective-C\n\n## CocoaPods\nInstall [CocoaPods] (http://cocoapods.org) and add the Gummi Injection reference to your Podfile\n\n```\nplatform :ios, '5.0'\n  pod 'Gummi-Injection'\nend\n```\n\n#### Add this remote\n\n```\n$ pod repo add sschmid-cocoapods-specs https://github.com/sschmid/cocoapods-specs\n```\n\n#### Install Gummi Injection\n\n```\n$ cd path/to/project\n$ pod install\n```\nOpen the created Xcode Workspace file.\n\n## Projects that use Gummi Injection\n* [Gummi Commander] (https://github.com/sschmid/Gummi-Commander) Event Command Mapping System for Objective-C","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsschmid%2Fgummi-injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsschmid%2Fgummi-injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsschmid%2Fgummi-injection/lists"}