Latest 2.1.4
Homepage https://github.com/Digipolitan/dependency-injector
License BSD
Platforms ios 8.0, osx 10.10, watchos 2.0, tvos 9.0, requires ARC
Authors

Swift Version
Build Status
CocoaPods Compatible
Carthage Compatible
Swift Package Manager Compatible
Platform
Twitter

Dependency injector Swift. Compatible for swift server-side and swift for iOS

Installation

CocoaPods

To install DependencyInjector with CocoaPods, add the following lines to your Podfile.

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

pod 'DependencyInjector'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate DependencyInjector into your Xcode project using Carthage, specify it in your Cartfile:

github 'Digipolitan/dependency-injector' ~> 2.0

Run carthage update to build the framework and drag the built DependencyInjector.framework into your Xcode project.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding DependencyInjector as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/Digipolitan/dependency-injector.git", from: "2.0.0")
]

The Basics

First you must create a Module and register some providers

let module = Module()
module.bind(IAnimal.self).to(Dog.self)

IAnimal is a protocol that MUST be implemented by the Dog class

public protocol IAnimal {

    var name: String { get }

    func scream() -> String
}

open class Dog: IAnimal, Injectable {

    public var name: String

    public required convenience init(injector: Injector, arguments: [String : Any]?) throws {
        self.init(name: arguments?["name"] as? String ?? "Athina")
    }

    init(name: String) {
        self.name = name
    }

    public func scream() -> String {
        return "Barking"
    }
}

After that, you must register your module inside an injector

Injector.default.register(module: module)

Finally, inject an IAnimal and retrieve a concrete class registered inside your module

if let animal = try? Injector.default.inject(IAnimal.self) {
  print(animal.name) // print Athina
  print(animal.scream()) // print Barking
}

Advanced

Arguments

Register a provider that handle arguments :

let module = Module()
module.bind(IAnimal.self).with { (_, arguments) -> IAnimal? in
  if let name = arguments?["name"] as? String {
    return Dog(name: name)
  }
  return nil
}
Injector.default.register(module: module)

Inject an IAnimal with arguments Dictionary<String, Any> :

if let animal = Injector.default.inject(IAnimal.self, arguments: ["name": "Athina"]) {
  print(animal.name) // print Athina
  print(animal.scream()) // print Barking
}
if let otherAnimal = Injector.default.inject(IAnimal.self, arguments: ["name": "Yoda"]) {
  print(otherAnimal.name) // print Yoda
  print(otherAnimal.scream()) // print Barking
}

Contributing

See CONTRIBUTING.md for more details!

This project adheres to the Contributor Covenant Code of Conduct.
By participating, you are expected to uphold this code. Please report
unacceptable behavior to [email protected].

License

DependencyInjector is licensed under the BSD 3-Clause license.

Latest podspec

{
    "name": "DependencyInjector",
    "version": "2.1.4",
    "summary": "Dependency injector made in pure swift",
    "homepage": "https://github.com/Digipolitan/dependency-injector",
    "license": {
        "type": "BSD",
        "file": "LICENSE"
    },
    "authors": {
        "Digipolitan": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Digipolitan/dependency-injector.git",
        "tag": "v2.1.4"
    },
    "source_files": "Sources/**/*.{swift,h}",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This