Photo courtesy of www.kevinandamanda.com
Dip
is a simple Dependency Injection Container.
It's aimed to be as simple as possible yet provide rich functionality usual for DI containers on other platforms. It's inspired by .NET
's Unity Container and other DI containers.
let container = DependencyContainer()
and registering your dependencies, by associating a protocol or type to a factory
using container.register { MyService() as Service }
.container.resolve() as Service
to resolve an instance of protocol or type using that DependencyContainer
.import Dip
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// Create the container
private let container = DependencyContainer { container in
// Register some factory. ServiceImp here implements protocol Service
container.register { ServiceImp() as Service }
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Resolve a concrete instance. Container will instantiate new instance of ServiceImp
let service = try! container.resolve() as Service
...
}
}
import Dip
class AppDelegate: UIResponder, UIApplicationDelegate {
private let container = DependencyContainer.configure()
...
}
//CompositionRoot.swift
import Dip
import DipUI
extension DependencyContainer {
static func configure() -> DependencyContainer {
return DependencyContainer { container in
unowned let container = container
DependencyContainer.uiContainers = [container]
container.register(tag: "ViewController") { ViewController() }
.resolvingProperties { container, controller in
controller.animationsFactory = try container.resolve() as AnimatonsFactory
}
container.register { AuthFormBehaviourImp(apiClient: $0) as AuthFormBehaviour }
container.register { container as AnimationsFactory }
container.register { view in ShakeAnimationImp(view: view) as ShakeAnimation }
container.register { APIClient(baseURL: NSURL(string: "http://localhost:2368")!) as ApiClient }
}
}
}
extension DependencyContainer: AnimationsFactory {
func shakeAnimation(view: UIView) -> ShakeAnimation {
return try! self.resolve(withArguments: view)
}
}
extension ViewController: StoryboardInstantiatable {}
//ViewController.swift
class ViewController {
var animationsFactory: AnimationsFactory?
private let _formBehaviour = Injected<AuthFormBehaviour>()
var formBehaviour: AuthFormBehaviour? {
return _formBehaviour.value
}
...
}
Dip is completely documented and comes with a Playground that lets you try all its features and become familiar with API. You can find it in Dip.xcworkspace
.
Note: it may happen that you will need to build Dip framework before playground will be able to use it. For that select
Dip
scheme and build for iPhone Simulator.
You can find bunch of usage examples and usfull tips in a wiki.
If your are using VIPER architecture - here is VIPER demo app that uses Dip instead of manual dependency injection.
There are also several blog posts that describe how to use Dip and some of its implementation details:
File an issue if you have any question. Pull requests are warmly welcome too.
You can install Dip using your favorite dependency manager:
pod "Dip"
github "AliSoftware/Dip"
To build for Swift 2.3 run Carthage with --toolchain com.apple.dt.toolchain.Swift_2_3
option.
.Package(url: "https://github.com/AliSoftware/Dip", majorVersion: 5, minor: 0)
On OSX you can run tests from Xcode. On Linux you need to have Swift Package Manager installed and use it to build and run tests using this command: swift build --clean && swift build && swift test
This library has been created by Olivier Halligon and is maintained by Ilya Puchka.
Dip is available under the MIT license. See the LICENSE
file for more info.
The animated GIF at the top of this README.md
is from this recipe on the yummy blog of Kevin & Amanda. Go try the recipe!
The image used as the SampleApp LaunchScreen and Icon is from Matthew Hine and is under CC-by-2.0.
link |
Stars: 972 |
Last commit: 4 weeks ago |
StoryboardInstantiatable
with SPM (#233).Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics