Latest 0.2.0
Homepage https://github.com/nicholascross/Injectable
License MIT
Platforms ios 11.0, osx 10.9, watchos 3.2, tvos 11.0, requires ARC

Injectable

build status
code coverage
Codacy Badge
carthage compatible
CocoaPods compatible
GitHub release
Swift 4.2.x
platforms

A Swift dependency injection container which minimises the need for centralised registration

Features

  • Supports dependency resolution without prior registration of the type with the container
  • Supports resolution of the same object based on object lifetime policy, persistent, transient or ephemeral
  • Supports both reference and value types
  • Supports resolution of multiple variants of the same type differentiated by key
  • Supports registration of interface types to allow for resolving dependencies where the implementing class is not directly available
  • Supports resolution of interdependant classes with cyclic dependencies

Trade-offs and limitations

  • Registrationless resolution requires visibility of the exact type being resolved
  • Resolving via an interface works around the need for visibility of the type being resolved at the expense of requiring registration, this registration is not enforced at compile time, meaning it can fail at runtime if the registration was ommitted
  • Coupling between dependency injection framework and injected types through protocol conformance

Usage examples

See more usage examples here

import Foundation
import XCTest
@testable import Injectable

private protocol Animal { }
private protocol Person {
    var pet: Animal { get }
}

private struct Cat: Animal, Injectable {
    static func create(inContainer container: Container, variant: String?) -> Cat {
        return Cat()
    }
}

private struct CatPerson: Person, Injectable {
    let pet: Animal

    static func create(inContainer container: Container, variant: String?) -> CatPerson {
        return CatPerson(pet: container.resolve() as Cat)
    }

    init(pet: Animal) {
        self.pet = pet
    }
}

class ReadMeTests: XCTestCase {

    private var container: DependencyContainer!

    override func setUp() {
        container = DependencyContainer()
    }

    func testResolve() {
        let catPerson: CatPerson = container.resolve()
        let person: Person = container.resolve() as CatPerson

        XCTAssertTrue(catPerson.pet is Cat)
        XCTAssertTrue(person.pet is Cat)
    }
}

Installation

Cocoapods

Add the following to you pod file

pod 'InjectableSwift'

Carthage

Add the following to your Cartfile

github "nicholascross/Injectable"

Latest podspec

{
    "name": "InjectableSwift",
    "version": "0.2.0",
    "summary": "A Swift dependency injection container",
    "description": "A Swift dependency injection container which minimises the need for centralised registration",
    "homepage": "https://github.com/nicholascross/Injectable",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": "Nicholas Cross",
    "social_media_url": "https://twitter.com/nickacross",
    "platforms": {
        "ios": "11.0",
        "osx": "10.9",
        "watchos": "3.2",
        "tvos": "11.0"
    },
    "source": {
        "git": "https://github.com/nicholascross/Injectable.git",
        "tag": "0.2.0"
    },
    "source_files": "Injectable/**/*.swift",
    "requires_arc": true,
    "swift_version": "4.2"
}

Pin It on Pinterest

Share This