{"id":18667847,"url":"https://github.com/alschmut/factorydependencyinjection","last_synced_at":"2025-07-27T22:38:45.097Z","repository":{"id":222286066,"uuid":"756810607","full_name":"alschmut/FactoryDependencyInjection","owner":"alschmut","description":"Dependency Injection with Factory","archived":false,"fork":false,"pushed_at":"2024-02-13T11:38:08.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-27T18:23:27.340Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alschmut.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2024-02-13T11:00:06.000Z","updated_at":"2024-02-13T11:01:52.000Z","dependencies_parsed_at":"2024-02-13T13:42:32.489Z","dependency_job_id":null,"html_url":"https://github.com/alschmut/FactoryDependencyInjection","commit_stats":null,"previous_names":["alschmut/factorydependencyinjection"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alschmut%2FFactoryDependencyInjection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alschmut%2FFactoryDependencyInjection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alschmut%2FFactoryDependencyInjection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alschmut%2FFactoryDependencyInjection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alschmut","download_url":"https://codeload.github.com/alschmut/FactoryDependencyInjection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239495561,"owners_count":19648350,"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":[],"created_at":"2024-11-07T08:40:19.424Z","updated_at":"2025-02-18T15:26:42.446Z","avatar_url":"https://github.com/alschmut.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dependency Injection with Factory\n\nTo decouple the dependency injection framework [Factory](https://github.com/hmlongco/Factory) from the internal app usage there are two options:\n\n\n## Using KeyPaths to access a defined service on one of possibly many containers\n\n### Service Usage\n```swift\nclass MyViewModel {\n    private let myService: MyServiceProtocol\n\n    init(\n        myService: any MyServiceProtocol = resolve(\\.myService)\n    ) {\n        self.myService = myService\n    }\n}\n```\n\n### Service definition\n```swift\nprotocol MyServiceProtocol {\n    func getSomething() -\u003e String\n}\n\nextension FactoryContainer {\n    var myService: FactoryAdapter\u003cMyServiceProtocol\u003e {\n        FactoryAdapter { MyService() }\n    }\n}\n\nstruct MyService: MyServiceProtocol {\n    func getSomething() -\u003e String {\n        return \"\"\n    }\n}\n```\n\n### FactoryAdapter\n```swift\nfunc resolve\u003cValue\u003e(_ factoryAdapter: KeyPath\u003cFactoryContainer, FactoryAdapter\u003cValue\u003e\u003e) -\u003e Value {\n    FactoryContainer.shared[keyPath: factoryAdapter]()\n}\n\nstruct FactoryContainer {\n    static let shared = FactoryContainer()\n}\n\nstruct FactoryAdapter\u003cValue\u003e {\n    let factory: Factory\u003cValue\u003e\n\n    init(\n        value: @escaping () -\u003e Value\n    ) {\n        factory = Factory(Container.shared) { value() }\n    }\n\n    init(\n        scope: FactoryScope,\n        value: @escaping () -\u003e Value\n    ) {\n        factory = Factory(Container.shared) { value() }.scope(Self.scope(from: scope))\n    }\n\n    func callAsFunction() -\u003e Value {\n        factory()\n    }\n\n    private static func scope(from scope: FactoryScope) -\u003e Scope {\n        switch scope {\n        case .singleton: return .singleton\n        case .cashed: return .cached\n        case .shared: return .shared\n        }\n    }\n}\n\nenum FactoryScope {\n    case singleton\n    case cashed\n    case shared\n}\n```\n\n## Using static variables on a single global container\n\n### Service Usage\n```swift\nclass MyViewModel {\n    private let myService: MyServiceProtocol\n\n    init(\n        myService: any MyServiceProtocol = resolve(.myService)\n    ) {\n        self.myService = myService\n    }\n}\n```\n\n### Service definition\n```swift\nprotocol MyServiceProtocol {\n    func getSomething() -\u003e String\n}\n\nextension FactoryAdapter\u003cMyServiceProtocol\u003e {\n    static let myService = FactoryAdapter {\n        MyService()\n    }\n}\n\nstruct MyService: MyServiceProtocol {\n    func getSomething() -\u003e String {\n        return \"\"\n    }\n}\n```\n\n### FactoryAdapter\n```swift\nfunc resolve\u003cValue\u003e(_ factoryAdapter: FactoryAdapter\u003cValue\u003e) -\u003e Value {\n    factoryAdapter()\n}\n\nstruct FactoryAdapter\u003cValue\u003e {\n    let factory: Factory\u003cValue\u003e\n\n    init(\n        value: @escaping () -\u003e Value\n    ) {\n        factory = Factory(Container.shared) { value() }\n    }\n\n    init(\n        scope: FactoryScope,\n        value: @escaping () -\u003e Value\n    ) {\n        factory = Factory(Container.shared) { value() }.scope(Self.scope(from: scope))\n    }\n\n    func callAsFunction() -\u003e Value {\n        factory()\n    }\n\n    private static func scope(from scope: FactoryScope) -\u003e Scope {\n        switch scope {\n        case .singleton: return .singleton\n        case .cashed: return .cached\n        case .shared: return .shared\n        }\n    }\n}\n\nenum FactoryScope {\n    case singleton\n    case cashed\n    case shared\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falschmut%2Ffactorydependencyinjection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falschmut%2Ffactorydependencyinjection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falschmut%2Ffactorydependencyinjection/lists"}