A Swift dependency injection / lookup framework for iOS
CarbonGraph is a Swift dependency injection / lookup framework for iOS. You can use it to build loose coupling between modules.
The CarbonGraph project contains 2 frameworks:
Framework | Description |
---|---|
CarbonCore | Focused specifically on core DI implementations |
CarbonObjC | CarbonCore’s ObjC adaptation framework |
CarbonCore Stable Version | Required iOS Version | Required Swift Version |
---|---|---|
1.2.2 - 1.3.2 | 9.0 + | 5.2 + |
1.3.3 | 9.0 + | 5.5 + |
CarbonObjC Version | CarbonCore Compatible Version |
---|---|
1.2.2 | 1.2.2 |
1.3.1 | 1.3.1 - 1.3.3 |
For more information see Compatibility
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate CarbonGraph into your Xcode project using CocoaPods, specify it in your Podfile:
pod ‘CarbonCore’, ‘~> 1.3.3’
let context = ObjectContext()
let definitionBuilder = Definition("filevc")
.protocol(UIViewController.self)
.object(FileViewController())
context.register(builder: definitionBuilder)
context[UIViewController.self, name: "filevc"]
let context = ObjectContext()
let definitionBuilder = Definition()
.object(FileManager() as FileManagerProtocol)
context.register(builder: definitionBuilder)
context[FileManagerProtocol.self]
class MyConfiguration: Configuration {
static func definitions(of context: ObjectContext) -> Definitions {
Definition()
.object(FilePath())
Definition()
.constructor(FileModel.init(filePath:))
}
}
let context = ObjectContext()
context.register(configuration: MyConfiguration.self)
context[FileModel.self]
Note: For convenience, the registration of object definitions and the creation of context that appear in the following will be omitted. All definitions in your project must be registered before they can be resolved.
Definition()
.protocol(FileViewControllerProtocol.self)
.constructor(FileViewController.init(fileManager:))
context[FileViewControllerProtocol.self].fileManager
Definition()
.protocol(FileViewControllerProtocol.self)
.object(FileViewController())
.property(\.fileManager)
context[FileViewControllerProtocol.self].fileManager
Definition()
.protocol(FileViewControllerProtocol.self)
.object(FileViewController())
.setter(FileViewController.setFileManager(fileManager:))
context[FileViewControllerProtocol.self].fileManager
Definition()
.factory(fileViewController(context:))
context[FileViewControllerProtocol.self].fileManager
static func fileViewController(context: ObjectContext) -> FileViewControllerProtocol {
let fileVC = FileViewController()
fileVC.fileManager = context[FileManagerProtocol]
return fileVC
}
The same as:
Definition()
.factory { context in
let fileVC = FileViewController()
fileVC.fileManager = context[FileManagerProtocol]
return fileVC as FileViewControllerProtocol
}
Definition()
.factory(fileModel(context:path:name:))
context[FileModelProtocol.self, "/china/beijing", "family.png"]
static func fileModel(context: ObjectContext, path: String, name: String) -> FileModelProtocol {
FileModel(path: path, name: name)
}
For more information see Basic usage
The documentation of this project is written in DocC, please clone the repo and build the documentation yourself.
For more information see Unit Test and Example
You are more than welcome to contribute code to the project,for more information see Contribution. You can also join the WeChat discussion group, check the QR code below.
CarbonGraph is released under the MIT license. See LICENSE for details.