Injection
is a tiny utility to help managing dependency injection.
Features:
Singleton
, dependencies that are only instantiated once and are shared amongst its usersLazySingleton
, dependencies that are only instantiated once, lazily, and are shared amongst its usersFactory
, dependencies that are instancied upon usage, unique to each userIf you're working directly in a Package, add Injection to your Package.swift file
dependencies: [
.package(url: "https://github.com/JARMourato/Injection.git", .upToNextMajor(from: "1.0.0" )),
]
If working in an Xcode project select File->Swift Packages->Add Package Dependency...
and search for the package name: Injection
or the git url:
https://github.com/JARMourato/Injection.git
import Injection
// A dependency that gets created every time it needs to be resolved, and therefore its lifetime is bounded to the instance that uses it
let reader = factory { RSSReader() }
// A dependency that gets created immediately and is shared throughout the lifetime of the application.
let database = singleton { Analytics() }
// A dependency that gets created only once, the first time it needs to be resolved and has the lifetime of the application.
let database = lazySingleton { Realm() }
// Syntatic sugar to combine dependencies to inject
let cacheModule = module {
singleton { ImageCache() }
factory { AudioCache() }
factory { VideoCache() }
}
import Injection
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
do {
try inject {
reader
database
cacheModule
}
} catch {
print("TODO: Handle error properly... \(error)")
}
return true
}
}
import Injection
class VideoPlayer: BackendProtocol {
// Will resolve the dependency immediately upon type instantiation
@Inject var database: Database
// The dependency only gets resolved on the first time the property gets accessed
@LazyInject var videoCache: VideoCache
// The functionality is similar to `LazyInject` except the property may or may not have been injected.
@OptionalInject var network: Network?
}
or via the initializer:
import Injection
struct Reader {
private let rssReader: RSSReader
init(rssReader: RSSReader = resolve()) {
self.rssReader = rssReader
}
}
If you feel like something is missing or you want to add any new functionality, please open an issue requesting it and/or submit a pull request with passing tests 🙌
MIT
João (@_JARMourato)
link |
Stars: 4 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics