Ject 1.2.2

Ject 1.2.2

TestsTested
LangLanguage SwiftSwift
License Custom
ReleasedLast Release Oct 2017
SwiftSwift Version 3.0.0
SPMSupports SPM

Maintained by Josh.



Ject 1.2.2

  • By
  • superuserdid

Ject




Swift

Ject is a simple, lightweight, quick solution for dependency injection for Swift.

It is designed with quick integration and set up in mind for the creative developer, ideal for small to medium sized projects. Ject takes inspiration from Dagger2, intended to provide simplicity over all for adding Dependency Injection to your project.

Ject is currently in Beta.

Requirements

  • Swift 3
  • iOS 8.0+

Getting Started

Creating a Dependency Graph

You can easily create a new DependencyManager using

let dependencyManager = DependencyManager()

Or you can define your own DependencyGraph implementation using

let dependencyManager = DependencyManager(MyDependencyGraph())

It is recommended to keep one reference to your Graph with your dependencies. A good solution is to create a property in struct that is lazily instantiated

import Ject

struct Dependencies {

    static var instance = Dependencies()

    lazy var manager = DependencyManager()

    private init(){
        //Default Constructor
    }
    
    ...
}

Making a class Injectable

In order to use Ject Dependency Injection, your dependencies must inherit from Injectable. Declare your class and have it implement Injectable from the Ject pod like so

class ViewUtils: Injectable {

    required init() {
        //Default Constructor
    }

    func inject(dependencyGraph: Graph) -> Injectable {
        return ViewUtils()
    }

    func isSingleton() -> Bool {
        return true
    }

}

Injecting Dependencies into Dependencies

Many times your dependencies have dependencies of their own. Using Ject, you can simple inject these dependencies using two methods:

  • Constructor Injection

Simply add your dependencies to your constructor as parameters as you normally would. Ideally these dependencies would also inherit from Injectable.

class ViewUtils: Injectable {

    var mIconManager: IconManager?

    var mColorManager: ColorManager?

    init(iconManager: IconManager, colorManager: ColorManager) {
        mIconManager = iconManager
        mColorManager = colorManager
    }

    required init() {
        //Default Constructor
    }

    func inject(dependencyGraph: Graph) -> Injectable {
        //Use your constructor here to inject your Injectable Dependencies
        return ViewUtils(iconManager: dependencyGraph.inject(IconManager.self), colorManager: dependencyGraph.inject(ColorManager.self))
    }

    func isSingleton() -> Bool {
        return true
    }

}
  • Property Injection

You can also inject dependencies directly into properties as follows

class ViewUtils: Injectable {

    var manager: DependencyManager {
        return Dependencies.instance.manager
    }

    var mIconManager: IconManager {
        return manager.inject(IconManager.self)
    }

    var mColorManager: ColorManager {
        return manager.inject(ColorManager.self)
    }

    required init() {
        //Default Constructor
    }

    func inject(dependencyGraph: Graph) -> Injectable {
        return ViewUtils()
    }

    func isSingleton() -> Bool {
        return true
    }

}

License

MIT license. See the LICENSE file for details.