Repolode
hmlongco

hmlongco/Factory

A Swift and SwiftUI dependency injection container that registers factories as simple computed properties, with scopes, contexts, and test mocking.

Stars
2,930
Forks
193
Open issues
1
Last commit
10 days ago

What is it?

Factory is a container-based dependency injection library for Swift and SwiftUI. You define a service as a computed property on a Container that returns a Factory, and then request it with an @Injected property wrapper, a direct function call, or a global dependency function. The compiler checks that a factory exists for every injected type, so missing registrations fail at build time rather than at runtime.

The library covers the common needs of DI systems: scopes such as singleton, cached, shared, and custom session scopes control how long instances live; contexts let you swap implementations for debug builds, unit tests, SwiftUI previews, or UI tests. Overriding a registration with a mock is a single call, which makes previews and XCTest or Swift Testing cases straightforward to set up.

Factory also works with Observation, @MainActor, and Swift concurrency isolation, and includes a debug mode that traces the resolution tree. It is distributed through Swift Package Manager under the MIT license.

Key features

  • 01Registers services as computed properties on a Container, with compile-time checks for missing factories.
  • 02Offers property wrappers, direct factory calls, and global dependency functions for resolution.
  • 03Supports scopes such as singleton, cached, shared, and custom scopes.
  • 04Provides contexts for debug, unit tests, SwiftUI previews, and UI tests to swap implementations.
  • 05Works with SwiftUI scene-based containers, Observation, and @MainActor isolation.
  • 06Includes a debug trace that prints the dependency resolution tree.

Use cases

  • Injecting services and repositories into SwiftUI views and view models.
  • Replacing network or analytics services with mocks in SwiftUI previews.
  • Writing unit tests that load, empty, and error states by overriding dependencies.
  • Structuring an iOS app with MVVM, MVP, Clean, or VIPER architectures.

Things to know

  • Only for Swift projects, so it does not apply to other languages.

In collections

Topics

FAQ

What is Factory?
A Swift and SwiftUI dependency injection container that registers factories as simple computed properties, with scopes, contexts, and test mocking. Factory is a container-based dependency injection library for Swift and SwiftUI. You define a service as a computed property on a Container that returns a Factory, and then request it with an @Injected property wrapper, a direct function call, or a global dependency function. The compiler checks that a factory exists for every injected type, so missing registrations fail at build time rather than at runtime.
Is Factory free to use? What license does it have?
Factory is open source under the MIT license. Check the license terms before using it in commercial products.
Is Factory still maintained?
Actively maintained. The last commit to Factory was on Oct 28, 2026.
What can I use Factory for?
Injecting services and repositories into SwiftUI views and view models. Replacing network or analytics services with mocks in SwiftUI previews. Writing unit tests that load, empty, and error states by overriding dependencies. Structuring an iOS app with MVVM, MVP, Clean, or VIPER architectures.
Where can I get Factory?
The source code and installation instructions are on GitHub: https://github.com/hmlongco/Factory

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects