ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - Liftric/DIKit: Dependency Injection Framework for Swift, inspired by KOIN.
Dependency Injection Framework for Swift, inspired by KOIN. - Liftric/DIKit
Visit Site

GitHub - Liftric/DIKit: Dependency Injection Framework for Swift, inspired by KOIN.

GitHub - Liftric/DIKit: Dependency Injection Framework for Swift, inspired by KOIN.

DIKit

Dependency Injection Framework for Swift, inspired by KOIN. 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. After installing Carthage just add DIKit to your Cartfile:

github "Liftric/DIKit" ~> 1.6.1

Via CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. After installing CocoaPods add DIKit to your Podfile:

platform :ios, '9.0'
pod 'DIKit', '~> 1.6.1'

Basic usage

  1. Define some sub DependencyContainer (basically some sort of module declaration):
import DIKit

public 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 }
    }
}
  1. Set the root DependencyContainer and set it before the application gets initialised:
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:

import DIKit

@UIApplicationMain
class AppDelegate: UIApplicationDelegate {
    override init() {
        super.init()
        DependencyContainer.defined(by: module {
            single { AppState() as AppStateProtocol }
            factory { StopWatch() as StopWatchProtocol }
        })
    }
}
  1. Inject the dependencies, for instance in a module:
import DIKit

class Backend: BackendProtocol {
    @Inject var network: NetworkProtocol
}

or a ViewController:

import DIKit

class 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:

import DIKit

struct AppState: AppStateProtocol {
    private let backend: BackendProtocol
    init(backend: BackendProtocol = resolve()) {
        self.backend = backend
    }
}

Advanced usage

Resolving by Tag

When 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.

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.

import DIKit

class 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
    }
}

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory