{"id":13465655,"url":"https://github.com/AliSoftware/Dip","last_synced_at":"2025-03-25T16:32:39.559Z","repository":{"id":1633609,"uuid":"43641724","full_name":"AliSoftware/Dip","owner":"AliSoftware","description":"Simple Swift Dependency container. Use protocols to resolve your dependencies and avoid singletons / sharedInstances!","archived":false,"fork":false,"pushed_at":"2024-03-20T18:15:27.000Z","size":1818,"stargazers_count":977,"open_issues_count":9,"forks_count":72,"subscribers_count":25,"default_branch":"develop","last_synced_at":"2024-10-14T16:41:11.237Z","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/AliSoftware.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2015-10-04T15:49:35.000Z","updated_at":"2024-09-24T08:42:05.000Z","dependencies_parsed_at":"2024-06-18T13:58:53.302Z","dependency_job_id":null,"html_url":"https://github.com/AliSoftware/Dip","commit_stats":{"total_commits":417,"total_committers":21,"mean_commits":"19.857142857142858","dds":0.5683453237410072,"last_synced_commit":"7ec008a5c09520b67f07669ad5a753d757f05bbe"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliSoftware%2FDip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliSoftware%2FDip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliSoftware%2FDip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliSoftware%2FDip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AliSoftware","download_url":"https://codeload.github.com/AliSoftware/Dip/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222088597,"owners_count":16928984,"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-07-31T15:00:33.383Z","updated_at":"2024-10-29T17:31:22.578Z","avatar_url":"https://github.com/AliSoftware.png","language":"Swift","readme":"# Dip\n\n[![CI Status](https://travis-ci.org/AliSoftware/Dip.svg?branch=develop)](https://travis-ci.org/AliSoftware/Dip)\n[![Version](https://img.shields.io/cocoapods/v/Dip.svg?style=flat)](http://cocoapods.org/pods/Dip)\n[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![License](https://img.shields.io/cocoapods/l/Dip.svg?style=flat)](http://cocoapods.org/pods/Dip)\n[![Platform](https://img.shields.io/cocoapods/p/Dip.svg?style=flat)](http://cocoapods.org/pods/Dip)\n[![Swift Version](https://img.shields.io/badge/Swift-4.0--4.2-F16D39.svg?style=flat)](https://developer.apple.com/swift)\n[![Swift Version](https://img.shields.io/badge/Linux-4.0--4.2-4BC51D.svg?style=flat)](https://developer.apple.com/swift)\n\n![Animated Dipping GIF](cinnamon-pretzels-caramel-dipping.gif)  \n_Photo courtesy of [www.kevinandamanda.com](http://www.kevinandamanda.com/recipes/appetizer/homemade-soft-cinnamon-sugar-pretzel-bites-with-salted-caramel-dipping-sauce.html)_\n\n## Introduction\n\n`Dip` 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 `.NET`'s [Unity Container](https://msdn.microsoft.com/library/ff647202.aspx) and other DI containers.\n\n* You start by creating `let container = DependencyContainer()` and **registering your dependencies, by associating a _protocol_ or _type_ to a `factory`** using `container.register { MyService() as Service }`.\n* Then you can call `container.resolve() as Service` to **resolve an instance of _protocol_ or _type_** using that `DependencyContainer`.\n* You can easily use Dip along with **Storyboards and Nibs** . There is also a **[code generator](https://github.com/ilyapuchka/dipgen)** that can help to simplify registering new components.\n\n\u003cdetails\u003e\n\u003csummary\u003eBasic usage\u003c/summary\u003e\n\n```swift\nimport Dip\n\n@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n    \n    // Create the container\n    private let container = DependencyContainer { container in\n    \n        // Register some factory. ServiceImp here implements protocol Service\n        container.register { ServiceImp() as Service }\n    }\n\n    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -\u003e Bool { \n        \n        // Resolve a concrete instance. Container will instantiate new instance of ServiceImp\n        let service = try! container.resolve() as Service\n    \n        ...\n    }\n}\n\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eMore sophisticated example\u003c/summary\u003e\n\n```swift\nimport Dip\n\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n\tprivate let container = DependencyContainer.configure()\n\t...\n}\n\n//CompositionRoot.swift\nimport Dip\nimport DipUI\n\nextension DependencyContainer {\n\n\tstatic func configure() -\u003e DependencyContainer {\n\t\treturn DependencyContainer { container in \n\t\t\tunowned let container = container\n\t\t\tDependencyContainer.uiContainers = [container]\n\t\t\n\t\t\tcontainer.register(tag: \"ViewController\") { ViewController() }\n\t\t\t  .resolvingProperties { container, controller in\n\t\t\t\t  controller.animationsFactory = try container.resolve() as AnimatonsFactory\n\t\t\t}\n    \n\t\t\tcontainer.register { AuthFormBehaviourImp(apiClient: $0) as AuthFormBehaviour }\n\t\t\tcontainer.register { container as AnimationsFactory }\n\t\t\tcontainer.register { view in ShakeAnimationImp(view: view) as ShakeAnimation }\n\t\t\tcontainer.register { APIClient(baseURL: NSURL(string: \"http://localhost:2368\")!) as ApiClient }\n\t\t}\n\t}\n\n}\n\nextension DependencyContainer: AnimationsFactory { \n    func shakeAnimation(view: UIView) -\u003e ShakeAnimation {\n        return try! self.resolve(withArguments: view)\n    }\n}\n\nextension ViewController: StoryboardInstantiatable {}\n\n//ViewController.swift\n\nclass ViewController {\n    var animationsFactory: AnimationsFactory?\n\n    private let _formBehaviour = Injected\u003cAuthFormBehaviour\u003e()\n    \n    var formBehaviour: AuthFormBehaviour? {\n        return _formBehaviour.value\n    }\n\t...\n}\n\n```\n\n\u003c/details\u003e\n\n## Documentation \u0026 Usage Examples\n\nDip is completely [documented](http://cocoadocs.org/docsets/Dip/5.0.0/) and comes with a Playground that lets you try all its features and become familiar with API. You can find it in `Dip.xcworkspace`.\n\n\u003e Note: it may happen that you will need to build Dip framework before playground will be able to use it. For that select `Dip` scheme and build for iPhone Simulator.\n\nYou can find bunch of usage examples and usfull tips in a [wiki](../../wiki). \n\nIf your are using [VIPER](https://www.objc.io/issues/13-architecture/viper/) architecture - [here](https://github.com/ilyapuchka/VIPER-SWIFT) is VIPER demo app that uses Dip instead of manual dependency injection.\n\nThere are also several blog posts that describe how to use Dip and some of its implementation details:\n\n- [IoC container in Swift](http://ilya.puchka.me/ioc-container-in-swift/)\n- [IoC container in Swift. Circular dependencies and auto-injection](http://ilya.puchka.me/ioc-container-in-swift-circular-dependencies-and-auto-injection/)\n- [Dependency injection with Dip](http://ilya.puchka.me/dependency-injecinjection-with-dip/)\n\nFile an issue if you have any question. Pull requests are warmly welcome too.\n\n\n## Features\n\n- **[Scopes](../../wiki/scopes)**. Dip supports 5 different scopes (or life cycle strategies): _Unique_, _Shared_, _Singleton_, _EagerSingleton_, _WeakSingleton_;\n- **[Auto-wiring](../../wiki/auto-wiring)** \u0026 **[Auto-injection](../../wiki/auto-injection)**. Dip can infer your components' dependencies injected in constructor and automatically resolve them as well as dependencies injected with properties.\n- **[Resolving optionals](../../wiki/resolving-optionals)**. Dip is able to resolve constructor or property dependencies defined as optionals.\n- **[Type forwarding](../../wiki/type-forwarding)**. You can register the same factory to resolve different types implemeted by a single class.\n- **[Circular dependencies](../../wiki/circular-dependencies)**. Dip will be able to resolve circular dependencies if you will follow some simple rules;\n- **[Storyboards integration](../../wiki/storyboards-integration)**. You can easily use Dip along with storyboards and Xibs without ever referencing container in your view controller's code;\n- **[Named definitions](../../wiki/named-definitions)**. You can register different factories for the same protocol or type by registering them with [tags]();\n- **[Runtime arguments](../../wiki/runtime-arguments)**. You can register factories that accept up to 6 runtime arguments (and extend it if you need);\n- **[Easy configuration](../../wiki/containers-collaboration)** \u0026 **Code generation**. No complex containers hierarchy, no unneeded functionality. Tired of writing all registrations by hand? There is a [cool code generator](https://github.com/ilyapuchka/dipgen) that will create them for you. The only thing you need is to annotate your code with some comments.\n- **Weakly typed components**. Dip can resolve \"weak\" types when they are unknown at compile time.\n- **Thread safety**. Registering and resolving components is thread safe;\n- **Helpful error messages and configuration validation**. You can validate your container configuration. If something can not be resolved at runtime Dip throws an error that completely describes the issue;\n\n\n## Installation\n\nYou can install Dip using your favorite dependency manager:\n\n\u003cdetails\u003e\n\u003csummary\u003eCocoaPods\u003c/summary\u003e\n\n`pod \"Dip\"`\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eCarthage\u003c/summary\u003e\n\n```\ngithub \"AliSoftware/Dip\"\n```\n\nTo build for Swift 2.3 run Carthage with `--toolchain com.apple.dt.toolchain.Swift_2_3` option.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eSwift Package Manager\u003c/summary\u003e\n\n```swift\n.Package(url: \"https://github.com/AliSoftware/Dip\", majorVersion: 5, minor: 0)\n```\n\n\u003c/details\u003e\n\n## Running tests\n\nOn OSX you can run tests from Xcode. On Linux you need to have Swift Package Manager installed and use it to build and run tests using this command: `swift build --clean \u0026\u0026 swift build \u0026\u0026 swift test`\n\n## Credits\n\nThis library has been created by [**Olivier Halligon**](olivier@halligon.net) and is maintained by [**Ilya Puchka**](https://twitter.com/ilyapuchka).\n\n**Dip** is available under the **MIT license**. See the `LICENSE` file for more info.\n\nThe animated GIF at the top of this `README.md` is from [this recipe](http://www.kevinandamanda.com/recipes/appetizer/homemade-soft-cinnamon-sugar-pretzel-bites-with-salted-caramel-dipping-sauce.html) on the yummy blog of [Kevin \u0026 Amanda](http://www.kevinandamanda.com/recipes/). Go try the recipe!\n\nThe image used as the SampleApp LaunchScreen and Icon is from [Matthew Hine](https://commons.wikimedia.org/wiki/File:Chocolate_con_churros_-_San_Ginés,_Madrid.jpg) and is under _CC-by-2.0_.\n","funding_links":[],"categories":["Libs","Dependency Injection [🔝](#readme)"],"sub_categories":["Dependency Injection","API"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAliSoftware%2FDip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAliSoftware%2FDip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAliSoftware%2FDip/lists"}