{"id":18809967,"url":"https://github.com/aryaxt/swiftinjection","last_synced_at":"2025-10-04T19:41:49.614Z","repository":{"id":56947586,"uuid":"56959326","full_name":"aryaxt/SwiftInjection","owner":"aryaxt","description":"Dependency Injection framework for Swift","archived":false,"fork":false,"pushed_at":"2017-10-07T17:38:56.000Z","size":85,"stargazers_count":22,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-21T08:28:04.153Z","etag":null,"topics":["dependency-injection","ioc","ioc-container","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aryaxt.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":"2016-04-24T07:18:36.000Z","updated_at":"2024-08-31T10:53:36.000Z","dependencies_parsed_at":"2022-08-21T08:20:46.881Z","dependency_job_id":null,"html_url":"https://github.com/aryaxt/SwiftInjection","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/aryaxt/SwiftInjection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FSwiftInjection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FSwiftInjection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FSwiftInjection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FSwiftInjection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aryaxt","download_url":"https://codeload.github.com/aryaxt/SwiftInjection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FSwiftInjection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278366609,"owners_count":25975091,"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","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["dependency-injection","ioc","ioc-container","swift"],"created_at":"2024-11-07T23:18:23.047Z","updated_at":"2025-10-04T19:41:49.595Z","avatar_url":"https://github.com/aryaxt.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftInjection\n[![Build Status](https://api.travis-ci.org/aryaxt/SwiftInjection.svg)](https://api.travis-ci.org/aryaxt/SwiftInjection)\n[![Version](http://cocoapod-badges.herokuapp.com/v/SwiftInjection/badge.png)](http://cocoadocs.org/docsets/SwiftInjection)\n\nA dependency container for Swift\n\n#### Setting up Dependencies\n\nA Module file is where you define your dependencies. The goal is to abstract out all your dependencies in this file. The only class in your project that should know about concrete implementations should be the module class, the rest of the classes in your application should be using these implementations through interfaces.\n\nYou could have multiple module classes in order to organize your dependencies\n```swift\npublic class AppModule: DIModule {\n\t\n\tpublic func load(container: DIContainer) {\n\t\tcontainer.bind() { UserDefaults.standard() }\n\t\tcontainer.bind() { URLSession.shared() }\n\t\tcontainer.bind(type: HttpService.self) { HttpClient(baseUrl: \"https://api.github.com\", urlSession: $0)) }\n\t\tcontainer.bind(type: GithubService.self) { GithubHttpClient(httpService: $0) }\n\t\tcontainer.bind(type: AnalyticsTracker.self, named: GoogleAnalyticsTracker.analyticsIdentifier()) { GoogleAnalyticsTracker() }\n\t\tcontainer.bind(type: AnalyticsTracker.self, named: AmplitudeAnalyticsTracker.analyticsIdentifier()) { AmplitudeAnalyticsTracker() }\n\t}\n\t\n}\n\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n\toverride init() {\n\t\tsuper.init()\n\t\tDIContainer.instance.addModule(AppModule())\n\t}\n}\n```\n\n#### Binding Internal classes\navoid direct use of singletons to make your code more testable\n```swift\ncontainer.bind() { URLSession.shared() }\n```\n#### Binding classes as singleton\nInstead of adding singleton logic to your classes simply bind them as singleton\nNote: Structs are not compatible with singleton pattern\n```swift\n// Bind class as singleton\nbind(asSingleton: true) { Session() }\n\n// Bind protocol to an implementation as singleton\nbind(AnalyticsTracker.self, asSingleton: true) { GoogleAnalyticsTracker() }\n```\n#### Bind Named Instances\nIn cases where you have multiple implementations for a single protocol you can use named binding to retrieve the correct instance\n```swift\nbind(AnalyticsTracker.self, named: \"Google\") { GoogleAnalyticsTraker() }\nbind(AnalyticsTracker.self, named: \"Amplitude\") { AmplitudeAnalyticsTracker() }\n\n// Inject expected instance\nlet googleAnalyticsTracker: AnalyticsTracker = inject(named: \"Google\")\nlet amplitudeAnalyticsTracker: AnalyticsTracker = inject(named: \"Amplitude\")\n\n// Get all implementations for a given protocol (great for chain of responssibilities)\nlet trackers: [AnalyticsTracker] = injectAll()\n```\n\n#### Property Injection\nOnly use property injection on root level, for anything else below the viewController use constructor injection\n```swift\nclass ViewController: UIViewController {\n\tlet githubService: GithubService = inject() // Injects the implementation defined in module\n\tlet session = inject(Session.self) // injects the singleton instance\n\tlet analyticTrackers: [AnalyticsTracker] = injectAll() // Injects all implemetations of AnalyticsTracker\n}\n```\n\n#### Constructor Injection\nSimpy pass dependencies through the intiializer and define binding in the module file\n```swift\nprotocol GithubService { }\n\nprotocol HttpService { }\n\nclass GithubHttpClient: GithubService {\n\tlet httpService: HttpService\n\t// Constructor injection\n\tinit(httpService: HttpService) {\n\t\tself.httpService = httpService\n\t}\n}\n\nclass AppModule: DIModule {\n\tfunc load(container: DIContainer) {\n\t\tcontainer.bind(type: URLSession.self) { URLSession.shared() }\n\t\tcontainer.bind(type: HttpService.self) { HttpClient(baseUrl: \"https://api.github.com\", urlSession: $0)) }\n\t\tcontainer.bind(type: GithubService.self) { GithubHttpClient(httpService: $0) }\n\t}\n}\n\nclass ViewController: UIViewController {\n\t// Property Injection\n\t// This will return an instance of GithubHttpClient with all depndencies as defined in module\n\tlet githubService: GithubService = inject()\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faryaxt%2Fswiftinjection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faryaxt%2Fswiftinjection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faryaxt%2Fswiftinjection/lists"}