Swiftpack.co - Package - nfam/sinjection.swift

SInjection

swift platform build codecov license

Simple Dependency Injection for Swift.

Installation

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/nfam/sinjection.git", majorVersion: 0, minor: 1)
    ]
)

Usage

import SInjection

protocol DatabaseProtocol { }

class Database: DatabaseProtocol { }
class BackupRepository: DatabaseProtocol { }

class Repository {
    let database: DatabaseProtocol
    init(database: DatabaseProtocol) {
        self.database = database
    }
}
class MirrorService {
    let database: DatabaseProtocol
    init(database: DatabaseProtocol) {
        self.database = database
    }
}

func f() {
    let container = Container()
    container.set(forType: DatabaseProtocol.self) { _ in
        return Database()
    }
    container.set(forType: DatabaseProtocol.self, "mirror") { _ in
        return MirrorDatabase()
    }
    container.set(forType: Repository.self) { r in
        return Repository(database: try r.resolve(forType: Database.self))
    }
    container.set(forType: MirrorService.self) { r in
        return MirrorService(database: try r.resolve(forType: DatabaseProtocol.self, "mirror"))
    }

    try container.build()
    let repository: Repository = container.get(forType: Repository.self)!
}

Github

link
Stars: 0
Help us keep the lights on

Dependencies

Used By

Total: 0