DIKit
Dependency Injection Framework for Swift, inspired by KOIN. Basically an implementation of service-locator pattern, living within the application's context.
Grow as you go!
We started small, it perfectly fits our use case.
Installation
Via Carthage
DIKit can be installed using Carthage. After installing Carthage just add DIKit to your Cartfile:
Via CocoaPods
CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. After installing CocoaPods add DIKit to your Podfile:
Basic usage
- Define some sub
DependencyContainer
(basically some sort of module declaration):
- Set the root
DependencyContainer
and set it before the application gets initialised:
Without sub DependencyContainer
the following shorthand writing also does the job:
- Inject the dependencies, for instance in a module:
or a ViewController
:
Injection via constructor:
Advanced usage
Resolving by Tag
When registering your dependencies you can optionally define a tag. The tag can be anything, as long as it is AnyHashable
.
This way you can register different resolvable dependencies for the same Type.
You can then reference the same tag when resolving the type and can thus resolve different instances. Referencing the tag works with all injection methods.