Injector

develop

A Swift package for simple dependency injection that also supports Swift UI previews.
ptrkstr/Injector




A Swift package for simple dependency injection that also supports Swift UI previews.

Features

  • Tiny footprint (Injector.swift and Resolver.swift are the only source files).
  • Supports SwiftUI previews.
  • Great unit tests.
  • Cool logo.

Usage

import Injector

// AppDelegate or App (SwiftUI)
Injector.setup {
    $0.register(Person(), mock: Person_Mock(), for: PersonType.self)
    $0.register(Cat(), mock: Cat_Mock(), for: CatType.self)
}

// App
let person = inject(PersonType.self) // Person
let cat = inject(CatType.self) // Cat

// Unit Test or SwiftUI Preview
let person = inject(PersonType.self) // Person_Mock
let cat = inject(CatType.self) // Cat_Mock

Why should my main app contain the mocks?

This is so mocks can be used in SwiftUI previews. I couldn't find an alternative way but would be glad to hear it if you had one.

Installation

SPM

Add the following to your project:

https://github.com/ptrkstr/injector

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

Last updated: Wed Aug 31 2022 20:23:09 GMT-0500 (GMT-05:00)