Latest 1.0.0
Homepage https://github.com/benjohnde/DIKit
License MIT
Platforms ios 9.0, requires ARC
Authors

Dependency Injection Framework for Swift, inspired by KOIN. Basically an implementation of service-locator pattern, living within the application’s context (through the AppDelegate).

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 "benjohnde/DIKit" ~> 1.0

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.0.0'

Basic usage

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

public extension DependencyContainer {
static var backend: DependencyContainer {
return DependencyContainer { container in
container.register(lifetime: .transient) { Backend() as BackendProtocol }
}
}
}

public extension DependencyContainer {
static var network: DependencyContainer {
return DependencyContainer { container in
container.register { Network() as NetworkProtocol }
}
}
}


2. Let `AppDelegate` adhere `DefinesContainer`:
```swift
import DIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, DefinesContainer {
    let container = DependencyContainer.derive(from: .backend, .network)
}
  1. Inject the dependencies, for instance in a module:
    
    import DIKit

class Backend: BackendProtocol {
let network: NetworkProtocol = inject()
}


or a `ViewController`:
```swift
import DIKit

class FirstViewController: UIViewController {
    // MARK: - Dependencies
    let backend: BackendProtocol = inject()

    // MARK: - View lifecycle
    override func viewWillAppear(_ animated: Bool) {
        let result = backend.fetch()
        print(result)
    }
}

Latest podspec

{
    "name": "DIKit",
    "version": "1.0.0",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "summary": "Dependency Injection Framework for Swift.",
    "homepage": "https://github.com/benjohnde/DIKit",
    "authors": {
        "Ben John": "[email protected]"
    },
    "social_media_url": "http://twitter.com/benjohnde",
    "platforms": {
        "ios": "9.0"
    },
    "swift_version": "4.2",
    "source": {
        "git": "https://github.com/benjohnde/DIKit.git",
        "tag": "1.0.0"
    },
    "source_files": "DIKit/Sources/**/*.{swift}",
    "requires_arc": true
}

Pin It on Pinterest

Share This