{"id":18419465,"url":"https://github.com/liftric/dikit","last_synced_at":"2025-04-07T13:31:34.826Z","repository":{"id":51266837,"uuid":"122620586","full_name":"Liftric/DIKit","owner":"Liftric","description":"Dependency Injection Framework for Swift, inspired by KOIN.","archived":false,"fork":false,"pushed_at":"2023-11-17T10:28:44.000Z","size":254,"stargazers_count":102,"open_issues_count":12,"forks_count":17,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-05T20:33:50.793Z","etag":null,"topics":["application-context","carthage","cocoapods","dependency","dependency-injection","dependency-injection-container","helper","injection","injection-container","injection-dependency","injection-framework","ios","koin","service-locator","swift","swift-package-manager","swift5","swiftpm"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/Liftric.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-02-23T12:48:14.000Z","updated_at":"2025-03-23T13:27:35.000Z","dependencies_parsed_at":"2024-01-02T23:55:25.013Z","dependency_job_id":null,"html_url":"https://github.com/Liftric/DIKit","commit_stats":null,"previous_names":["benjohnde/dikit"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liftric%2FDIKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liftric%2FDIKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liftric%2FDIKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liftric%2FDIKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Liftric","download_url":"https://codeload.github.com/Liftric/DIKit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247661743,"owners_count":20975109,"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":["application-context","carthage","cocoapods","dependency","dependency-injection","dependency-injection-container","helper","injection","injection-container","injection-dependency","injection-framework","ios","koin","service-locator","swift","swift-package-manager","swift5","swiftpm"],"created_at":"2024-11-06T04:17:08.823Z","updated_at":"2025-04-07T13:31:34.314Z","avatar_url":"https://github.com/Liftric.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DIKit\n\nDependency Injection Framework for Swift, inspired by [KOIN](https://insert-koin.io/). Basically an implementation of service-locator pattern, living within the application's context.\n\n\u003e Grow as you go!\n\nWe started small, it perfectly fits our use case.\n\n## Installation\n\n### Via Carthage\n\nDIKit can be installed using [Carthage](https://github.com/Carthage/Carthage). After installing Carthage just add DIKit to your Cartfile:\n\n```ogdl\ngithub \"Liftric/DIKit\" ~\u003e 1.6.1\n```\n\n### Via CocoaPods\n\n[CocoaPods](http://cocoapods.org) is a dependency manager for Swift and Objective-C Cocoa projects. After installing CocoaPods add DIKit to your Podfile:\n\n```ruby\nplatform :ios, '9.0'\npod 'DIKit', '~\u003e 1.6.1'\n```\n\n## Basic usage\n\n1. Define some sub `DependencyContainer` (basically some sort of module declaration):\n```swift\nimport DIKit\n\npublic extension DependencyContainer {\n    static var backend = module {\n        single { Backend() as BackendProtocol }\n    }\n}\n\npublic extension DependencyContainer {\n    static var network = module {\n        single { Network() as NetworkProtocol }\n    }\n}\n\npublic extension DependenyContainer {\n    static var app = module {\n        single { AppState() as AppStateProtocol }\n        factory { StopWatch() as StopWatchProtocol }\n    }\n}\n```\n\n2. Set the root `DependencyContainer` and set it before the application gets initialised:\n```swift\nimport DIKit\n\n@UIApplicationMain\nclass AppDelegate: UIApplicationDelegate {\n    override init() {\n        super.init()\n        DependencyContainer.defined(by: modules { .backend; .network; .app })\n    }\n}\n```\n\nWithout sub `DependencyContainer` the following shorthand writing also does the job:\n\n```swift\nimport DIKit\n\n@UIApplicationMain\nclass AppDelegate: UIApplicationDelegate {\n    override init() {\n        super.init()\n        DependencyContainer.defined(by: module {\n            single { AppState() as AppStateProtocol }\n            factory { StopWatch() as StopWatchProtocol }\n        })\n    }\n}\n```\n\n3. Inject the dependencies, for instance in a module:\n```swift\nimport DIKit\n\nclass Backend: BackendProtocol {\n    @Inject var network: NetworkProtocol\n}\n```\n\nor a `ViewController`:\n```swift\nimport DIKit\n\nclass FirstViewController: UIViewController {\n    // MARK: - Dependencies\n    @LazyInject var backend: BackendProtocol\n    @OptionalInject var stopwatch: StopWatchProtocol?\n\n    // MARK: - View lifecycle\n    override func viewWillAppear(_ animated: Bool) {\n        let result = backend.fetch()\n        print(result)\n    }\n}\n```\n\nInjection via constructor:\n\n```swift\nimport DIKit\n\nstruct AppState: AppStateProtocol {\n    private let backend: BackendProtocol\n    init(backend: BackendProtocol = resolve()) {\n        self.backend = backend\n    }\n}\n```\n\n## Advanced usage\n### Resolving by Tag\n\nWhen registering your dependencies you can optionally define a tag. The tag can be anything, as long as it is `AnyHashable`.\n\nThis way you can register different resolvable dependencies for the same Type.\n\n```swift\nenum StorageContext: String {\n    case userdata\n    case systemdata\n}\n\npublic extension DependencyContainer {\n    static var app = module {\n        factory(tag: StorageContext.systemdata) { LocalStorage() as LocalStorageProtocol }\n        factory(tag: StorageContext.userdata) { LocalStorage() as LocalStorageProtocol }\n    }\n}\n```\n\nYou can then reference the same tag when resolving the type and can thus resolve different instances. Referencing the tag works with all injection methods.\n\n```swift\nimport DIKit\n\nclass Backend: BackendProtocol {\n    @Inject(tag: StorageContext.systemdata) var injectedStorage: LocalStorageProtocol\n    @LazyInject(tag: StorageContext.systemdata) var lazyInjectedStorage: LocalStorageProtocol\n    @OptionalInject(tag: StorageContext.systemdata) var optionalInjectedStorage: LocalStorageProtocol?\n    \n    private let constructorInjectedStorage: LocalStorageProtocol\n    init(storage: LocalStorageProtocol = resolve(tag: StorageContext.systemdata)) {\n        self.constructorInjectedStorage = storage\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliftric%2Fdikit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliftric%2Fdikit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliftric%2Fdikit/lists"}