Latest 1.2.2
Homepage https://github.com/wallflyrepo/Ject
License Apache
Platforms ios 8.0
Authors

Version
License
Platform
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

  • [x] Swift 3
  • [x] iOS 8.0+

Getting Started

Installation with CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Ject into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Ject'

Then, run the following command:

$ pod install

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.

Latest podspec

{
    "name": "Ject",
    "version": "1.2.2",
    "summary": "Simple Dependency Injection",
    "description": "TODO: Add long description of the pod here.",
    "homepage": "https://github.com/wallflyrepo/Ject",
    "license": {
        "type": "Apache",
        "file": "LICENSE"
    },
    "authors": {
        "superuserdid": "[email protected]"
    },
    "source": {
        "git": "https://github.com/wallflyrepo/Ject.git",
        "tag": "1.2.2"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Ject/Classes/**/*",
    "pushed_with_swift_version": "3.0.0"
}

Pin It on Pinterest

Share This