Latest 0.8
License MIT
Platforms ios 8.0, requires ARC
Frameworks Foundation

Build Status

A dependency container for Swift

Setting up Dependencies

A Module file is where you define your dependencies. The goal is to abstract out all your dependencies in this file. The only class in your project that should know about concrete implementations should be the module class, the rest of the classes in your application should be using these implementations through interfaces.

You could have multiple module classes in order to organize your dependencies

public class AppModule: DIModule {

    public func load(container: DIContainer) {
        container.bind() { UserDefaults.standard() }
        container.bind() { URLSession.shared() }
        container.bind(type: HttpService.self) { HttpClient(baseUrl: "", urlSession: $0)) }
        container.bind(type: GithubService.self) { GithubHttpClient(httpService: $0) }
        container.bind(type: AnalyticsTracker.self, named: GoogleAnalyticsTracker.analyticsIdentifier()) { GoogleAnalyticsTracker() }
        container.bind(type: AnalyticsTracker.self, named: AmplitudeAnalyticsTracker.analyticsIdentifier()) { AmplitudeAnalyticsTracker() }


class AppDelegate: UIResponder, UIApplicationDelegate {
    override init() {

Binding Internal classes

avoid direct use of singletons to make your code more testable

container.bind() { URLSession.shared() }

Binding classes as singleton

Instead of adding singleton logic to your classes simply bind them as singleton
Note: Structs are not compatible with singleton pattern

// Bind class as singleton
bind(asSingleton: true) { Session() }

// Bind protocol to an implementation as singleton
bind(AnalyticsTracker.self, asSingleton: true) { GoogleAnalyticsTracker() }

Bind Named Instances

In cases where you have multiple implementations for a single protocol you can use named binding to retrieve the correct instance

bind(AnalyticsTracker.self, named: "Google") { GoogleAnalyticsTraker() }
bind(AnalyticsTracker.self, named: "Amplitude") { AmplitudeAnalyticsTracker() }

// Inject expected instance
let googleAnalyticsTracker: AnalyticsTracker = inject(named: "Google")
let amplitudeAnalyticsTracker: AnalyticsTracker = inject(named: "Amplitude")

// Get all implementations for a given protocol (great for chain of responssibilities)
let trackers: [AnalyticsTracker] = injectAll()

Property Injection

Only use property injection on root level, for anything else below the viewController use constructor injection

class ViewController: UIViewController {
    let githubService: GithubService = inject() // Injects the implementation defined in module
    let session = inject(Session.self) // injects the singleton instance
    let analyticTrackers: [AnalyticsTracker] = injectAll() // Injects all implemetations of AnalyticsTracker

Constructor Injection

Simpy pass dependencies through the intiializer and define binding in the module file

protocol GithubService { }

protocol HttpService { }

class GithubHttpClient: GithubService {
    let httpService: HttpService
    // Constructor injection
    init(httpService: HttpService) {
        self.httpService = httpService

class AppModule: DIModule {
    func load(container: DIContainer) {
        container.bind(type: URLSession.self) { URLSession.shared() }
        container.bind(type: HttpService.self) { HttpClient(baseUrl: "", urlSession: $0)) }
        container.bind(type: GithubService.self) { GithubHttpClient(httpService: $0) }

class ViewController: UIViewController {
    // Property Injection
    // This will return an instance of GithubHttpClient with all depndencies as defined in module
    let githubService: GithubService = inject()

Latest podspec

    "name": "SwiftInjection",
    "version": "0.8",
    "summary": "A dependency injection framework for swift",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "License.txt"
    "authors": {
        "Aryan Ghassemi": ""
    "source": {
        "git": "",
        "tag": "0.8"
    "platforms": {
        "ios": "8.0"
    "source_files": "SwiftInjection/*.{swift}",
    "frameworks": "Foundation",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"

Pin It on Pinterest

Share This