{"id":25082226,"url":"https://github.com/niksativa/dikit","last_synced_at":"2026-02-27T00:05:11.655Z","repository":{"id":39404673,"uuid":"290461335","full_name":"NikSativa/DIKit","owner":"NikSativa","description":"Swift library that allows you to use a dependency injection pattern in your project by creating a container that holds all the dependencies in one place","archived":false,"fork":false,"pushed_at":"2025-09-15T16:27:25.000Z","size":155,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-21T23:52:14.546Z","etag":null,"topics":["assembly","container","dependencies","dependency","dependency-injection","injection","ios","lazy","propertywrapper","swift","swiftui","uikit"],"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/NikSativa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-08-26T10:04:26.000Z","updated_at":"2025-10-23T12:00:08.000Z","dependencies_parsed_at":"2023-11-07T01:11:36.794Z","dependency_job_id":"0af0ecec-cf11-4d32-81f4-5326e3e416c6","html_url":"https://github.com/NikSativa/DIKit","commit_stats":null,"previous_names":["niksativa/ninject"],"tags_count":65,"template":false,"template_full_name":null,"purl":"pkg:github/NikSativa/DIKit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikSativa%2FDIKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikSativa%2FDIKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikSativa%2FDIKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikSativa%2FDIKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NikSativa","download_url":"https://codeload.github.com/NikSativa/DIKit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikSativa%2FDIKit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29878283,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T23:51:21.483Z","status":"ssl_error","status_checked_at":"2026-02-26T23:50:46.793Z","response_time":89,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["assembly","container","dependencies","dependency","dependency-injection","injection","ios","lazy","propertywrapper","swift","swiftui","uikit"],"created_at":"2025-02-07T05:29:08.766Z","updated_at":"2026-02-27T00:05:11.631Z","avatar_url":"https://github.com/NikSativa.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DIKit - Dependency Injection Kit\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FNikSativa%2FDIKit%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/NikSativa/DIKit)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FNikSativa%2FDIKit%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/NikSativa/DIKit)\n[![CI](https://github.com/NikSativa/DIKit/actions/workflows/swift_macos.yml/badge.svg)](https://github.com/NikSativa/DIKit/actions/workflows/swift_macos.yml)\n\nSwift library that allows you to use a dependency injection pattern in your project by creating a container that holds all the dependencies in one place.\n\n## Create container\n\nMost recommended way to create a container is to use assemblies. Assemblies are classes that conform to `Assembly` protocol and are responsible for registering dependencies in the container.\n \n```swift\nContainer(assemblies: [\n    FoundationAssembly(),\n    APIAssembli(),\n    DataBaseAssembly(),\n    ThemeAssembly()\n])\n```\n\n## SwiftUI \n\nIf you want to use DIKit in SwiftUI you can create a container in the `App` struct like this:\n\n```swift\n@main\nstruct MyApp: App {\n    let container = Container(assemblies: [...])\n\n    var body: some Scene {\n        WindowGroup {\n            ContentView()\n                .environmentObject(container.toObservable())\n        }\n    }\n}\n\n// somewhere in the code\nstruct ContentView: View {\n    @DIObservedObject var api: API\n    \n    var body: some View {\n        Button(\"Make request\") {\n            api.request()\n        }\n    }\n}\n```\n\n## Shared container\n\nIf you want to use the container in property wrappers (InjectLazy, InjectProvider..) you can create a shared container like this:\n\n```swift\nlet container = Container(assemblies: [...])\ncontainer.makeShared()  // \u003c-- make container shared\n\n// somewhere in the code\nfinal class SomeManager {\n    @InjectLazy var api: API\n    \n    func makeRequest() {\n        api.request()\n    }\n}\n```\n\n## Create assembly\n\nMost basic assembly should look like this:\n```swift\nfinal class ApplicationAssembly: Assembly {\n    // list of assemblies that this assembly depends on\n    var dependencies: [Assembly] { \n        return [\n            AnalyticsAssembly(),\n            RouterAssembly(),\n            StoragesAssembly(),\n            ThemeAssembly(),\n            UIComponentsAssembly()\n        ]\n    }\n\n    func assemble(with registrator: Registrator) {\n        registrator.register(UserDefaults.self) {\n            return UserDefaults.standard\n        }\n\n        registrator.register(NotificationCenter.self) {\n            return NotificationCenter.default\n        }\n\n        registrator.register(UIApplication.self, options: .transient) {\n            return UIApplication.shared\n        }\n\n        registrator.register(BuildMode.self, entity: BuildMode.init)\n        \n        registrator.register(UserManager.self, options: .container, entity: UserManager.init)\n    }\n}\n```\n\n## How to resolve dependencies\n\nAt any place where you have access to container you can resolve dependencies like this:\n```swift\nlet api: API = container.resolve()\nlet dataBase: DataBase = container.resolve()\n```\nor if container is shared you can use:\n```swift\n@InjectLazy var api: API\n@InjectProvider var dataBase: DataBase\n```\nof SwiftUI:\n```swift\n@DIObservedObject var api: API\n@DIStateObject var viewState: ReCreatedState\n@DIProvider var dataBase: DataBase\n```\n\n## Registration\nMost basic registration looks like this:\n```swift\nregistrator.register(BuildMode.self, entity: BuildMode.init)\n```\n\n### options\n- `container` - creates a single instance and stores it in the container (like a singleton)\n- `weak` - resolve weak reference and if it was deallocated it will be resolved again\n- `transient` - resolve new instance every time, never store it in the container\n\n### 'Named' option\nYou can register multiple instances of the same type with different names and resolve them by name.\n\n```swift\nregistrator.register(Theme.self, name: \"light\") {\n    return LightTheme()\n}\n\nregistrator.register(Theme.self, name: \"dark\") {\n    return DarkTheme()\n}\n```\nand resolve it like this:\n```swift\nlet lightTheme: Theme = container.resolve(name: \"light\")\nlet darkTheme: Theme = container.resolve(name: \"dark\")\n\n@InjectLazy(named: \"light\") var lightTheme: Theme\n@InjectLazy(named: \"dark\") var darkTheme: Theme\n```\n\n### '.implements'\nMultiple implementations of different protocols in one class\n```swift\nregistrator.register(UserDefaults.self) {\n    return UserDefaults.standard\n}\n.implements(DefaultsStorage.self)\n```\n\n### Arguments\nYou can pass arguments to the registration block\n\nwhen you don’t know the index of the argument, but you are sure that there is only one type in the arguments:\n```swift\nregistrator.register(BlaBla.self, options: .transient) { _, args in\n    return BlaBla(name: args.first()) \u003c-- by type\n}\n```\nwhen you know index of argument:\n```swift\nregistrator.register(BlaBla.self, options: .transient) { _, args in\n    return BlaBla(name: args[1]) \u003c-- by index\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniksativa%2Fdikit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniksativa%2Fdikit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniksativa%2Fdikit/lists"}