Swiftpack.co - Infomaniak/swift-dependency-injection as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Infomaniak.
Infomaniak/swift-dependency-injection 2.0.1
Minimalist dependency injection mechanism
⭐️ 4
🕓 3 weeks ago
.package(url: "https://github.com/Infomaniak/swift-dependency-injection.git", from: "2.0.1")

InfomaniakDI

Minimalist dependency injection mechanism written in pure Swift.

Support any first party Swift platfom. [ iOS / iPadOS / watchOS / macOS / Linux ]

Well tested. Used in production across the Infomaniak iOS Apps.

Property wrapper based with @LazyInjectService and @InjectService.

Optimised to work well with SwiftUI Views.

Features

  • ☑ Efficient SwiftUI. @(Lazy)InjectService will not re-resolve, when used as a property, on a View redraw.
  • ☑ Good integration test coverage
  • ☑ Pure Swift Type Support
  • ☑ Thread safety
  • ☑ Injection by name
  • ☑ Injection with Arguments
  • ☑ Lazy init (with @LazyInjectService)

Roadmap

  • ☐ Optionals
  • ☐ Multiple containers

Requirements

  • Swift 5.6 +
  • SPM

Setup

Early on in the lifecycle of your app, you want to write something like this :

import InfomaniakDI

[…]

let factory = Factory(type: SomeService.self) { _, _ in
   SomeService()
}

do {
    try SimpleResolver.sharedResolver.store(factory: factory)
}
catch {
    FatalError("unexpected DI error \(error)")
}

later on, when you want to resolve a service use the property wrapper like so:

@InjectService var injected: SomeService

Injection will be performed at the init time of the owner of the property.

Use @LazyInjectService for resolution at first use of the wrapped property. Prefer @LazyInjectService when used as a property.

Documentation

Checkout ITSimpleReslover.swift for more advanced examples.

Licence

This package is available under the permissive ApacheV2 licence for you to enjoy.

GitHub

link
Stars: 4
Last commit: 1 week ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

2.0.1 - Compatibility for SPM 5.6
3 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics