Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Liftric/DIKit
Dependency Injection Framework for Swift, inspired by KOIN.
https://github.com/Liftric/DIKit
application-context carthage cocoapods dependency dependency-injection dependency-injection-container helper injection injection-container injection-dependency injection-framework ios koin service-locator swift swift-package-manager swift5 swiftpm
Last synced: about 1 month ago
JSON representation
Dependency Injection Framework for Swift, inspired by KOIN.
- Host: GitHub
- URL: https://github.com/Liftric/DIKit
- Owner: Liftric
- License: mit
- Created: 2018-02-23T12:48:14.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-17T10:28:44.000Z (about 1 year ago)
- Last Synced: 2024-09-16T23:39:54.185Z (3 months ago)
- Topics: application-context, carthage, cocoapods, dependency, dependency-injection, dependency-injection-container, helper, injection, injection-container, injection-dependency, injection-framework, ios, koin, service-locator, swift, swift-package-manager, swift5, swiftpm
- Language: Swift
- Homepage:
- Size: 248 KB
- Stars: 103
- Watchers: 5
- Forks: 17
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-swift - DIKit - Dependency Injection Framework for Swift, inspired by KOIN. (Libs / Dependency Injection)
- awesome-swift - DIKit - Dependency Injection Framework for Swift, inspired by KOIN. (Libs / Dependency Injection)
- awesome-swift - DIKit - Dependency Injection Framework for Swift, inspired by KOIN. (Dependency Injection [🔝](#readme))
- awesome-swift - DIKit - Dependency Injection Framework for Swift, inspired by KOIN. (Libs / Dependency Injection)
README
# DIKit
Dependency Injection Framework for Swift, inspired by [KOIN](https://insert-koin.io/). Basically an implementation of service-locator pattern, living within the application's context.
> Grow as you go!
We started small, it perfectly fits our use case.
## Installation
### Via Carthage
DIKit can be installed using [Carthage](https://github.com/Carthage/Carthage). After installing Carthage just add DIKit to your Cartfile:
```ogdl
github "Liftric/DIKit" ~> 1.6.1
```### Via CocoaPods
[CocoaPods](http://cocoapods.org) is a dependency manager for Swift and Objective-C Cocoa projects. After installing CocoaPods add DIKit to your Podfile:
```ruby
platform :ios, '9.0'
pod 'DIKit', '~> 1.6.1'
```## Basic usage
1. Define some sub `DependencyContainer` (basically some sort of module declaration):
```swift
import DIKitpublic extension DependencyContainer {
static var backend = module {
single { Backend() as BackendProtocol }
}
}public extension DependencyContainer {
static var network = module {
single { Network() as NetworkProtocol }
}
}public extension DependenyContainer {
static var app = module {
single { AppState() as AppStateProtocol }
factory { StopWatch() as StopWatchProtocol }
}
}
```2. Set the root `DependencyContainer` and set it before the application gets initialised:
```swift
import DIKit@UIApplicationMain
class AppDelegate: UIApplicationDelegate {
override init() {
super.init()
DependencyContainer.defined(by: modules { .backend; .network; .app })
}
}
```Without sub `DependencyContainer` the following shorthand writing also does the job:
```swift
import DIKit@UIApplicationMain
class AppDelegate: UIApplicationDelegate {
override init() {
super.init()
DependencyContainer.defined(by: module {
single { AppState() as AppStateProtocol }
factory { StopWatch() as StopWatchProtocol }
})
}
}
```3. Inject the dependencies, for instance in a module:
```swift
import DIKitclass Backend: BackendProtocol {
@Inject var network: NetworkProtocol
}
```or a `ViewController`:
```swift
import DIKitclass FirstViewController: UIViewController {
// MARK: - Dependencies
@LazyInject var backend: BackendProtocol
@OptionalInject var stopwatch: StopWatchProtocol?// MARK: - View lifecycle
override func viewWillAppear(_ animated: Bool) {
let result = backend.fetch()
print(result)
}
}
```Injection via constructor:
```swift
import DIKitstruct AppState: AppStateProtocol {
private let backend: BackendProtocol
init(backend: BackendProtocol = resolve()) {
self.backend = backend
}
}
```## Advanced usage
### Resolving by TagWhen registering your dependencies you can optionally define a tag. The tag can be anything, as long as it is `AnyHashable`.
This way you can register different resolvable dependencies for the same Type.
```swift
enum StorageContext: String {
case userdata
case systemdata
}public extension DependencyContainer {
static var app = module {
factory(tag: StorageContext.systemdata) { LocalStorage() as LocalStorageProtocol }
factory(tag: StorageContext.userdata) { LocalStorage() as LocalStorageProtocol }
}
}
```You can then reference the same tag when resolving the type and can thus resolve different instances. Referencing the tag works with all injection methods.
```swift
import DIKitclass Backend: BackendProtocol {
@Inject(tag: StorageContext.systemdata) var injectedStorage: LocalStorageProtocol
@LazyInject(tag: StorageContext.systemdata) var lazyInjectedStorage: LocalStorageProtocol
@OptionalInject(tag: StorageContext.systemdata) var optionalInjectedStorage: LocalStorageProtocol?
private let constructorInjectedStorage: LocalStorageProtocol
init(storage: LocalStorageProtocol = resolve(tag: StorageContext.systemdata)) {
self.constructorInjectedStorage = storage
}
}
```