Skip to content

Infomaniak/swift-dependency-injection

Repository files navigation

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.

About

Minimalist dependency injection mechanism

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages