Skip to content
forked from Liftric/DIKit

Dependency Injection Framework for Swift.

License

Notifications You must be signed in to change notification settings

ishansharma/DIKit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DIKit

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

Basic usage

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

public extension DependencyContainer {
    static var backend = module {
        factory { Backend() as BackendProtocol }
    }
}

public extension DependencyContainer {
    static var network = module {
        single { Network() as NetworkProtocol }
    }
}
  1. Let AppDelegate adhere DefinesContainer:
import DIKit

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

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

or a ViewController:

import DIKit

class FirstViewController: UIViewController {
    // MARK: - Dependencies
    @Injectable var backend: BackendProtocol

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

About

Dependency Injection Framework for Swift.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 93.1%
  • Objective-C 4.4%
  • Ruby 2.5%