{"id":20278940,"url":"https://github.com/chakrit/swift-ioc","last_synced_at":"2025-10-10T17:09:25.487Z","repository":{"id":25022167,"uuid":"28441547","full_name":"chakrit/swift-ioc","owner":"chakrit","description":"Minimal pure-Swift IOC/DI container implementation.","archived":false,"fork":false,"pushed_at":"2018-06-29T17:15:07.000Z","size":17,"stargazers_count":18,"open_issues_count":2,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-07-01T14:05:02.490Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/chakrit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-24T07:57:02.000Z","updated_at":"2024-05-08T18:42:12.000Z","dependencies_parsed_at":"2022-08-22T23:20:12.876Z","dependency_job_id":null,"html_url":"https://github.com/chakrit/swift-ioc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chakrit/swift-ioc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakrit%2Fswift-ioc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakrit%2Fswift-ioc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakrit%2Fswift-ioc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakrit%2Fswift-ioc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chakrit","download_url":"https://codeload.github.com/chakrit/swift-ioc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakrit%2Fswift-ioc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004815,"owners_count":26083783,"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-10T02:00:06.843Z","response_time":62,"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":[],"created_at":"2024-11-14T13:27:18.042Z","updated_at":"2025-10-10T17:09:25.438Z","avatar_url":"https://github.com/chakrit.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SWIFT IOC\n\nThis is a simple Swift IOC container implementation. The entire implementation is\ncontained (no pun intended) in a single\n[`Contents.swift`](https://raw.githubusercontent.com/chakrit/swift-ioc/master/SwiftIOC.playground/Contents.swift)\nfile for quick copy-and-paste into your project.\n\n# FEATURES\n\n* Simple API. Minimal concepts.\n* Tiny file. Minimal code (less than 100 LOC w/o docs).\n* One file drop-in. You don't even need any dependency manager.\n* Immutable containers. Construct new ones by `+`-ing existing ones.\n* Can be used to make hierarchical container trees.\n\n# USAGE\n\nRegister dependencies with either a `singleton` or a `factory` resolver:\n\n```swift\n// Registers a singleton object.\nlet resolver = singleton({ _ in GlobalServiceObject() })\n\n// Registers a factory function.\nlet resolver = factory({ (resolver) -\u003e ComplexObject in\n    let co = ComplexObject()\n    co.innerDependency = \u003c-resolver\n    co.invokeMethod()\n\n    return co\n})\n\n// Registers a factory function using Swift shorthand lambda form\nlet resolver = factory({ Dependent(dependency: \u003c-$0) })\n```\n\nConstruct your container by merging `Resolver`s together using the plus (`+`) operator:\n\n```swift\nlet container = singleton({ _ in ServiceOne() }) +\n  singleton({ _ in ServiceTwo() }) +\n  factory({ _ in InstanceService() })\n\n// or mutable\nvar container = emptyContainer()\ncontainer += singleton({ _ in ServiceOne() })\ncontainer += singleton({ _ in ServiceTwo() })\n//...\n```\n\nThen to obtain dependencies, pull it out from the container you have just created using\nthe reverse arrow (`\u003c-`) operator:\n\n```swift\nimport UIKit\n\nclass MyViewController: ViewController {\n  let serviceObject: GlobalServiceObject = \u003c-container\n  let anotherService: InstanceService = \u003c-container\n\n  override func viewDidLoad() {\n    super.viewDidLoad()\n    serviceObject.use()\n  }\n}\n```\n\nAdditionally, dependencies can be lazy:\n\n```swift\nclass X {\n  lazy var cash: ExpensiveObject = \u003c-container\n\n  func doWork() {\n    cash.work()\n  }\n}\n```\n\nOr hierarchical:\n\n```swift\nimport UIKit\n\nlet topLevel = singleton({ _ in ServiceObject() })\n\nclass RootViewController: ViewController {\n  let container = topLevel + singleton({ _ in ViewUtility() })\n\n  // ...\n}\n```\n\nOr registered out-of-order:\n\n```swift\nvar container = emptyContainer()\ncontainer += factory({ One(requireTwo: \u003c-$0 })\ncontainer += factory({ Two(requireThree: \u003c-$0 })\ncontainer += factory({ Three() })\n```\n\nEnjoy!\n\n# TODOs / PR material\n\n* Handles superclass types.\n* Overrides.\n* Modules system.\n* Cyclic check.\n\n# SUPPORT\n\nGitHub issue or ping me @chakrit on twitter.\n\n# LICENSE\n\nMIT\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchakrit%2Fswift-ioc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchakrit%2Fswift-ioc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchakrit%2Fswift-ioc/lists"}