talira

Factory (Swift)

Container-based dependency injection for Swift and SwiftUI

open sourceMITfree

Factory is a container-based dependency injection library for Swift and SwiftUI. You register a service as a computed property on a container in one line, and resolve it with the @Injected property wrapper or by calling the factory; a missing registration fails at compile time. It supports scopes, passed parameters, contexts, decorators and overrides for unit tests and SwiftUI Previews, with no build phases or code generation, in under 1,000 lines of code. Version 3 imports as FactoryKit and ships through Swift Package Manager only.

Problems it solves

  • inject dependencies into my SwiftUI views and view models
  • swap real services for mocks in my iOS unit tests and SwiftUI previews
  • manage service lifetimes in a Swift app without a heavy DI framework

Features

  • one-line registrations on a container
  • @Injected property wrappers and direct factory calls
  • compile-time safe: a missing factory doesn't compile
  • scopes, parameters, contexts and decorators
  • overrides for unit tests and SwiftUI Previews
  • no build phases or code generation

Limitations

  • Version 3 supports only Swift Package Manager; CocoaPods users must stay on 2.5.3 or embed the source.1

Alternatives

The same job

These do what Factory (Swift) does.

  1. swift-dependencies A Swift dependency management library inspired by SwiftUI's environment

Compare Factory (Swift) with the top 3