Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chakrit/swift-ioc
Minimal pure-Swift IOC/DI container implementation.
https://github.com/chakrit/swift-ioc
Last synced: about 20 hours ago
JSON representation
Minimal pure-Swift IOC/DI container implementation.
- Host: GitHub
- URL: https://github.com/chakrit/swift-ioc
- Owner: chakrit
- License: mit
- Created: 2014-12-24T07:57:02.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-06-29T17:15:07.000Z (over 6 years ago)
- Last Synced: 2024-04-15T12:21:33.971Z (7 months ago)
- Language: Swift
- Size: 16.6 KB
- Stars: 17
- Watchers: 6
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# SWIFT IOC
This is a simple Swift IOC container implementation. The entire implementation is
contained (no pun intended) in a single
[`Contents.swift`](https://raw.githubusercontent.com/chakrit/swift-ioc/master/SwiftIOC.playground/Contents.swift)
file for quick copy-and-paste into your project.# FEATURES
* Simple API. Minimal concepts.
* Tiny file. Minimal code (less than 100 LOC w/o docs).
* One file drop-in. You don't even need any dependency manager.
* Immutable containers. Construct new ones by `+`-ing existing ones.
* Can be used to make hierarchical container trees.# USAGE
Register dependencies with either a `singleton` or a `factory` resolver:
```swift
// Registers a singleton object.
let resolver = singleton({ _ in GlobalServiceObject() })// Registers a factory function.
let resolver = factory({ (resolver) -> ComplexObject in
let co = ComplexObject()
co.innerDependency = <-resolver
co.invokeMethod()return co
})// Registers a factory function using Swift shorthand lambda form
let resolver = factory({ Dependent(dependency: <-$0) })
```Construct your container by merging `Resolver`s together using the plus (`+`) operator:
```swift
let container = singleton({ _ in ServiceOne() }) +
singleton({ _ in ServiceTwo() }) +
factory({ _ in InstanceService() })// or mutable
var container = emptyContainer()
container += singleton({ _ in ServiceOne() })
container += singleton({ _ in ServiceTwo() })
//...
```Then to obtain dependencies, pull it out from the container you have just created using
the reverse arrow (`<-`) operator:```swift
import UIKitclass MyViewController: ViewController {
let serviceObject: GlobalServiceObject = <-container
let anotherService: InstanceService = <-containeroverride func viewDidLoad() {
super.viewDidLoad()
serviceObject.use()
}
}
```Additionally, dependencies can be lazy:
```swift
class X {
lazy var cash: ExpensiveObject = <-containerfunc doWork() {
cash.work()
}
}
```Or hierarchical:
```swift
import UIKitlet topLevel = singleton({ _ in ServiceObject() })
class RootViewController: ViewController {
let container = topLevel + singleton({ _ in ViewUtility() })// ...
}
```Or registered out-of-order:
```swift
var container = emptyContainer()
container += factory({ One(requireTwo: <-$0 })
container += factory({ Two(requireThree: <-$0 })
container += factory({ Three() })
```Enjoy!
# TODOs / PR material
* Handles superclass types.
* Overrides.
* Modules system.
* Cyclic check.# SUPPORT
GitHub issue or ping me @chakrit on twitter.
# LICENSE
MIT