Skip to main content

Swift Dependencies

The Swift Dependencies framework provides a concise and flexible way to manage dependencies in Swift applications. Designed with modularity and testability in mind, it integrates seamlessly into modern Swift architectures and ensures robust dependency injection.

Details

Repository URL: 🔗 Swift Dependencies on GitHub

Documentation: 🔗 Official Documentation

Authors: Point-Free

Tags:
dependency-management, injection, modularity, Swift, architecture

Features​

  • Dependency injection: Provides an intuitive API for dependency resolution and injection.
  • Modularity: Easily manage and decouple application dependencies.
  • Testability: Enable precise control of dependencies during testing.
  • Lightweight: Minimal impact on project complexity with a lightweight design.

Installation​

Using Swift Package Manager (SPM)​

To add Swift Dependencies to your project:

  1. Open your project in Xcode.
  2. Navigate to File > Add Packages.
  3. Paste the repository URL:
    https://github.com/pointfreeco/swift-dependencies
  4. Choose your desired version and click "Add Package".

Alternatively, add it directly to your Package.swift:

dependencies: [
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "0.1.0")
]

Core Concepts​

1. Dependency Container​

Use a centralized container to manage all dependencies.

import Dependencies

@Dependency(\.date) var dateProvider: () -> Date

2. Defining Dependencies​

Define dependencies within DependencyValues to ensure consistency across the application.

extension DependencyValues {
var date: () -> Date {
get { self[DateKey.self] }
set { self[DateKey.self] = newValue }
}
}

private enum DateKey: DependencyKey {
static let liveValue: () -> Date = Date.init
}

3. Injecting Dependencies​

Inject custom or mock dependencies for testing or alternative configurations.

let container = DependencyValues {
$0.date = { Date(timeIntervalSince1970: 0) }
}

Advantages​

  • Scalability: Scales well for applications of all sizes.
  • Testability: Facilitates dependency injection for unit and integration tests.
  • Customizability: Easily swap dependencies for different environments.
  • Consistency: Centralized dependency management ensures consistency.

Resources​

Explore Swift Dependencies→