{"id":16853161,"url":"https://github.com/devxoul/swinjectfactory","last_synced_at":"2025-04-11T07:10:32.238Z","repository":{"id":54914131,"uuid":"96819635","full_name":"devxoul/SwinjectFactory","owner":"devxoul","description":"Experimental Swinject extension for factory injection.","archived":false,"fork":false,"pushed_at":"2017-07-10T21:01:43.000Z","size":13,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T17:55:50.596Z","etag":null,"topics":["factory","swinject"],"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/devxoul.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}},"created_at":"2017-07-10T20:44:18.000Z","updated_at":"2022-08-08T23:49:39.000Z","dependencies_parsed_at":"2022-11-29T13:20:27.011Z","dependency_job_id":null,"html_url":"https://github.com/devxoul/SwinjectFactory","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FSwinjectFactory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FSwinjectFactory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FSwinjectFactory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FSwinjectFactory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devxoul","download_url":"https://codeload.github.com/devxoul/SwinjectFactory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239576548,"owners_count":19662109,"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":["factory","swinject"],"created_at":"2024-10-13T13:50:00.009Z","updated_at":"2025-02-19T00:31:24.306Z","avatar_url":"https://github.com/devxoul.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwinjectFactory\n\n![Swift](https://img.shields.io/badge/Swift-3.1-orange.svg)\n[![CocoaPods](http://img.shields.io/cocoapods/v/SwinjectFactory.svg)](https://cocoapods.org/pods/SwinjectFactory)\n[![Build Status](https://travis-ci.org/devxoul/SwinjectFactory.svg?branch=master)](https://travis-ci.org/devxoul/SwinjectFactory)\n[![Codecov](https://img.shields.io/codecov/c/github/devxoul/SwinjectFactory.svg)](https://codecov.io/gh/devxoul/SwinjectFactory)\n\nSwinject extension for factory injection.\n\n## Background\n\nImagine that your component has a runtime parameter (`userID`) which cannot be determined statically.\n\n```swift\nclass UserDetailViewController {\n  init(userID: Int, userService: UserServiceType, otherService: OtherServiceType)\n}\n```\n\nSwinject also supports arguments but Swift compiler cannot check the arguments statically. It can cause a human mistake.\n\n```swift\n// it takes 1 parameter\ncontainer.register(UserDetailViewController.self) { r, userID in\n  return UserDetailViewController(userID: userID, userService: r.resolve(UserServiceType.self)!)\n}\n\n// when pass 3 parameters it will return nil\ncontainer.resolve(UserServiceType.self, arguments: 1, 2, 3)\n```\n\nSo in this case we shoulud use a factory instead:\n\n```swift\n// register UserDetailViewController factory\ncontainer.register(((userID: Int) -\u003e UserDetailViewController).self) { r in\n  return { userID in\n    return UserDetailViewController(userID: userID, userService: r.resolve(UserServiceType.self)!)\n  }\n}\n\n// register a component that has a dependency to UserDetailViewController\ncontainer.register(UserListViewController.self) { r in\n  return UserListViewController(detailFactory: r.resolve(((userID: Int) -\u003e UserDetailViewController).self)!)\n}\n```\n\nBut it looks really complicated. SwinjectFactory makes it really clean.\n\n## At a Glance\n\nThe code above becomes short and readable with SwinjectFactory.\n\n```swift\n// register UserDetailViewController factory\ncontainer.register(factory: UserDetailViewController.self)\n\n// register a component that has a dependency to UserDetailViewController\ncontainer.register(UserListViewController.self) { r in\n  UserListViewController(detailFactory: r.resolve(factory: UserDetailViewController.self)!)\n}\n```\n\nIt can be even shorter with [SwinjectAutoregistration](https://github.com/Swinject/SwinjectAutoregistration).\n\n```swift\ncontainer.register(factory: UserDetailViewController.self)\ncontainer.autoregister(UserListViewControllerType.self, initializer: UserListViewController.init)\n```\n\n## Usage\n\nConform your component to `FactoryComponent`. This protocol requires a single static variable: `factory`. This is a curried function which takes a `Swinject.Resolver` as a first function parameter. The nested function takes a runtime parameter which is required for constructing a component class. It finally returns a service type.\n\n```swift\nclass UserDetailViewController: FactoryComponent {\n  init(userID: Int, userService: UserServiceType) {\n  }\n\n  // (Resolver) -\u003e (Runtime Paramters) -\u003e Service\n  static var factory: (Resolver) -\u003e (_ userID: Int) -\u003e UserDetailViewController {\n    return self.autocreate(UserDetailViewController.init) // it will automatically fill parameters\n  }\n}\n```\n\nHere is an example of using `UserDetailViewController` as a dependency:\n\n```swift\nclass UserListViewController {\n  let detailViewControllerFactory: UserDetailViewController.Factory\n\n  init(detailViewControllerFactory: UserDetailViewController.Factory) {\n    self.detailViewControllerFactory = detailViewControllerFactory\n  }\n  \n  func presentDetailViewController(userID: Int) {\n    let viewController = self.detailViewControllerFactory(userID)\n    self.navigationController?.push(viewController, animated: true)\n  }\n}\n```\n\n## Installation\n\nSwinjectFactory currently support [CocoaPods](https://cocoapods.org) only.\n\n```ruby\npod 'SwinjectFactory'\n```\n\n## Contributing\n\nAny discussions and pull requests are welcomed 💖\n\nTo create a Xcode project:\n\n```bash\n$ make project\n```\n\nThis will automatically create a Xcode project file and configure code generating environment.\n\n## License\n\nSwinjectFactory is under MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevxoul%2Fswinjectfactory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevxoul%2Fswinjectfactory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevxoul%2Fswinjectfactory/lists"}