Skip to content

Lightweight dependency injection framework for Swift (inspired by Swinject)

License

Notifications You must be signed in to change notification settings

marekpridal/SwinjectLight

Repository files navigation

SwinjectLight

Build Test platforms Swift Package Manager compatible GitHub GitHub All Releases

SwinjectLight is framework for lightweight dependency injection which works on any platform supporting Swift. Framework took inspiration from original Swinject implementation but removes some more complex functionality which is usually not necessary.

Installation

To add a package dependency to your Xcode project, select File > Add Package Dependency and https://github.com/marekpridal/SwinjectLight.

Alternatively in Package.swift add the following

// swift-tools-version:5.10

import PackageDescription

let package = Package(
  name: "SwinjectLightExample",
  dependencies: [
    .package(url: "https://github.com/marekpridal/SwinjectLight", from: "1.0.0")
  ],
  targets: [
    .target(name: "SwinjectLightExample", dependencies: ["SwinjectLight"])
  ]
)

Usage

import SwinjectLight

// Create container
let container = Container()

// Register singleton dependency
container.register(Session.self) { r in
    DefaultSession.shared
}

// Register instance based dependency
container.register(Api.self) { r in
    Networking(session: r.resolve(Session.self))
}

// Resolve dependency
let api = container.resolve(Api.self)

You can also check out demo project in repo for further details about usage.