{"id":3213,"url":"https://github.com/sabirvirtuoso/Kraken","last_synced_at":"2025-08-03T13:32:20.867Z","repository":{"id":56918036,"uuid":"97110333","full_name":"sabirvirtuoso/Kraken","owner":"sabirvirtuoso","description":"Simple Dependency Injection container for Swift. Use protocols to resolve dependencies with easy-to-use syntax!","archived":false,"fork":true,"pushed_at":"2017-02-28T13:16:19.000Z","size":174,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-10T04:38:25.230Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"mahbub-morshed/Kraken","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sabirvirtuoso.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-13T10:25:37.000Z","updated_at":"2020-10-09T02:44:12.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/sabirvirtuoso/Kraken","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabirvirtuoso%2FKraken","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabirvirtuoso%2FKraken/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabirvirtuoso%2FKraken/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabirvirtuoso%2FKraken/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sabirvirtuoso","download_url":"https://codeload.github.com/sabirvirtuoso/Kraken/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228548567,"owners_count":17935221,"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-01-05T20:16:34.652Z","updated_at":"2024-12-07T01:30:44.277Z","avatar_url":"https://github.com/sabirvirtuoso.png","language":"Swift","funding_links":[],"categories":["WebSocket","Dependency Injection","Injection"],"sub_categories":["Other free courses","Getting Started","Web View"],"readme":"# Kraken\n\n[![Build Status](https://travis-ci.org/sabirvirtuoso/Kraken.svg?branch=master)](https://travis-ci.org/sabirvirtuoso/Kraken)\n[![Code Climate](https://codeclimate.com/github/sabirvirtuoso/Kraken/badges/gpa.svg)](https://codeclimate.com/github/sabirvirtuoso/Kraken)\n[![Version](https://img.shields.io/cocoapods/v/Kraken.svg?style=flat)](http://cocoapods.org/pods/Kraken)\n[![License](https://img.shields.io/cocoapods/l/Kraken.svg?style=flat)](http://cocoapods.org/pods/Kraken)\n[![Platform](https://img.shields.io/cocoapods/p/Kraken.svg?style=flat)](http://cocoapods.org/pods/Kraken)\n[![Swift Version](https://img.shields.io/badge/Swift-3.0-F16D39.svg?style=flat)](https://developer.apple.com/swift)\n\n![Kraken GIF](Kraken.gif)\n_Photo courtesy of [www.krakenstudios.blogspot.com](http://krakenstudios.blogspot.com/)_\n\n## Introduction\n\n`Kraken` is a simple **Dependency Injection Container**.\n\nIt's aimed to be as simple as possible yet provide rich functionality usual for DI containers on other platforms. It's inspired by [Dip](https://github.com/AliSoftware/Dip) and some other DI containers.\n\n* You start by creating a `Dependency Configurator` for bootstrapping and **registering your dependencies, by associating a _protocol_ or _type_ to either an `implementation type`, an `implementation` or a `factory`**. It is preferrable to call your `Dependency Configurator` from `main.swift`.\n* Then you can call `inject(typeToInject)` to **resolve an instance of _protocol_ or _type_** based on the bootstrapping in your `Dependency Configurator`.\n\n## Documentation\n\n`Kraken` is yet to be documented fully but it comes with a sample project that lets you try all its features and become familiar with the API. You can find it in `Trigger.xcworkspace`.\n\nFile an issue if you have any question.\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n## Features\n\n- **Scopes**. Kraken supports 3 different scopes (or life cycle strategies): _Prototype_, _Singleton_, _EagerSingleton_;\n- **Named definitions**. You can register an `implementation type`, an `implementation` or a `factory` for a protocol or type;\n- **Runtime arguments**. You can register factories that accept up to 3 runtime arguments (You can create an extension to increase number of runtime arguments);\n- **Multiple definitions**. You can register multiple `implementation types`, `implementations` or `factories` per type or protocol;\n- **Circular dependencies**. Kraken can resolve circular dependencies;\n- **Auto-wiring**. Kraken can infer your components' dependencies injected in constructor and automatically resolve them.\n- **Easy configuration**. No complex container hierarchy, no unneeded functionality;\n- **Thread safety**. Registering and resolving components is thread safe;\n- **Helpful error messages**. If something can not be resolved at runtime Kraken throws an error that completely describes the issue;\n\n## Basic usage\n\nCreate a Dependency Configurator file where you bootstrap your dependencies much like the example shown below:\n\n```swift\nimport Kraken\n\nclass DependencyConfigurator {\n\n  static func bootstrapDependencies() {\n\n    // Register a protocol or type by providing its implementation type\n    Kraken.register(ServiceA.self, implementationType: ServiceAImpl.self, scope: .singleton)\n\n    // Register same protocol or type with different dependency by providing tag that can be either Int, String or Custom Type conforming to DependencyTagConvertible\n    Kraken.register(ServiceA.self, tag: CustomTag.One, implementationType: CustomFactory.self, scope: .singleton)\n\n    // Register a protocol or type by providing its implementation\n    Kraken.register(ServiceC.self, implementation: dummyImplementation, scope: .singleton)\n\n    // Register a protocol or type having weak property to allow Kraken to handle circular dependencies\n    // An example of such protocol (ServiceB) is given below\n    Kraken.register(ServiceB.self, implementationType: ServiceBImpl.self, scope: .singleton) {\n      (resolvedInstance: Injectable) -\u003e () in\n\n      let serviceB = resolvedInstance as! ServiceBImpl\n      serviceB.serviceA = injectWeak(ServiceA.self).value as! ServiceAImpl\n    }\n\n    // Register a protocol or type having runtime arguments to be injected in constructor\n    Kraken.register(ServiceD.self) {\n      ServiceDImpl(host: $0, port: $1, serviceB: inject(ServiceB.self)) as ServiceD\n    }\n\n    // Register generic protocols or types\n    Kraken.register(GenericDataSource\u003cServiceAImpl\u003e.self, implementationType: ServiceAImplDataSource.self, scope: .eagerSingleton)\n\n    // Register a protocol or type whose components' dependencies are injected automatically by container\n    Kraken.register(ServiceE.self) {\n      ServiceEImpl(serviceA: $0, serviceB: $1, serviceC: $2)\n    }\n  }\n}\n\n```\n\n```swift\nimport Kraken\n\nprotocol ServiceB: Injectable {\n\n  weak var serviceA: ServiceA? { get set }\n\n  var serviceC: ServiceC { get set }\n\n  var serviceBImplDataSource: GenericDataSource\u003cServiceBImpl\u003e { get set }\n\n  func myCompanyB() -\u003e String\n\n}\n\n```\n\nIt is worth mentioning that the protocols or types which are registered must conform to the `Injectable` protocol in order to be resolved by the container as shown in the example below:\n\n```swift\nimport Kraken\n\nprotocol ServiceA: Injectable {\n\n  func myCompanyA() -\u003e String\n\n}\n\n```\n\nIf you want to register multiple dependencies per type or protocol, you have to register with tag conforming to `DependencyTagConvertible`. You can declare a custom enum for this as shown below:\n\n```swift\nimport Kraken\n\nenum CustomTag: Int, DependencyTagConvertible {\n    case One = 1, Two\n}\n\n```\n\nAfter bootstrapping dependencies, its injection is as simple as invoking `inject()` which can be of different types as shown below:\n\n```swift\nimport Kraken\n\n// Inject dependency whose implementation was registered\nlet serviceC: ServiceC = inject(ServiceC.self)\n\n// Inject dependency whose implementation type was registered\nlet serviceA: ServiceA = inject(ServiceA.self)\n\n// Inject dependency whose implementation type was registered with tag\nlet serviceA: ServiceA = inject(ServiceA.self, tag: CustomTag.One)\n\n// Inject dependency providing runtime arguments\nlet serviceD: ServiceD = inject(ServiceD.self, withArguments: \"localhost\", 8080)\n\n// Inject dependency which is resolved by container through AutoWiring\nlet serviceE: ServiceE = inject(ServiceE.self)\n\n```\n\n## Installation\n\nKraken is built with Swift 3.0.\n\n#### CocoaPods\n\nKraken is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'Kraken', '1.5.0'\n```\n\n#### Manually\n1. Download and drop ```/Kraken``` folder in your project.\n2. Congratulations!\n\n## Author\n\nSyed Sabir Salman-Al-Musawi, sabirvirtuoso@gmail.com\n\nI'd also like to thank [**Sharafat Ibn Mollah Mosharraf**](sharafat_8271@yahoo.co.uk) for his big support during the development phase.\n\n**Kraken** is available under the **MIT license**. See the `LICENSE` file for more info.\n\nThe GIF at the top of this `README.md` is from [www.krakenstudios.blogspot.com](http://krakenstudios.blogspot.com/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabirvirtuoso%2FKraken","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsabirvirtuoso%2FKraken","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabirvirtuoso%2FKraken/lists"}