SeanROlszewski / AutoInjector

A dead simple dependency injector in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AutoInjector

A dead simple dependency injector in Swift with scoped bindings, ala Blindside

Example

import UIKit
import AutoInjector

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var injector = Injector()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        registerDependencies()

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = injector.getInstance(for: ViewController.self)
        window?.makeKeyAndVisible()

        return true
    }

    private func registerDependencies() {
        injector.addDependency(for: Guitar.self, withInstance: Guitar(brand: "Gibson"))

        injector.addDependency(for: ViewController.self) {
            injector in
            let vc = ViewController(nibName: String.from(type: ViewController.self), bundle: nil)
            vc.guitar = injector.getInstance(for: Guitar.self)
            return vc
        }
    }
}

About

A dead simple dependency injector in Swift


Languages

Language:Swift 98.4%Language:Objective-C 1.6%