1 Star 0 Fork 0

百度开源 / CarbonGraph

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Logo Banner Light Logo Banner Dark

Github Workflow  Swift Version  CocoaPods Version  Platforms  License

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

Features

  • Complete dependency injection capabilities
  • Complete Objective-C support
  • Convenient object definition DSL
  • High-performance thread-safe solution
  • Support resolving native Swift types
  • Support resolving with external parameters
  • Support resolving with circular dependencies
  • Automatic scanning of configuration
  • Additional module life cycle management capabilities

Requirements

  • CarbonCore Requirements
CarbonCore Stable Version Required iOS Version Required Swift Version
1.2.2, 1.3.1 9.0 5.2
  • CarbonObjC Version Compatibility
CarbonObjC Version CarbonCore Compatible Version
1.2.2 1.2.2
1.3.1 1.3.1

For more information see Compatibility

Installation

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.1'

Quick Start

  • Basic object registration and resolving
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]
  • Use configuration batch registration
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]

Basic usage

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.

  • Use constructor for dependency injection
Definition()
    .protocol(FileViewControllerProtocol.self)
    .constructor(FileViewController.init(fileManager:))
context[FileViewControllerProtocol.self].fileManager
  • Use property for dependency injection
Definition()
    .protocol(FileViewControllerProtocol.self)
    .object(FileViewController())
    .property(\.fileManager)
context[FileViewControllerProtocol.self].fileManager
  • Use setter for dependency injection
Definition()
    .protocol(FileViewControllerProtocol.self)
    .object(FileViewController())
    .setter(FileViewController.setFileManager(fileManager:))
context[FileViewControllerProtocol.self].fileManager
  • Use static factory for manual dependency injection
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
    }
  • Create objects with external parameters
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

Documentation

The documentation of this project is written in DocC, please clone the repo and build the documentation yourself.

  1. Clone this repo
  2. Open Carbon.xcworkspace with Xcode
  3. Product > Build Documentation

For more information see Unit Test and Example

Contribution

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.

 

License

CarbonGraph is released under the MIT license. See LICENSE for details.

MIT License Copyright (c) 2022 Baidu, Inc. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A Swift dependency injection / lookup framework for iOS 展开 收起
Swift 等 4 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Swift
1
https://gitee.com/baidu/CarbonGraph.git
git@gitee.com:baidu/CarbonGraph.git
baidu
CarbonGraph
CarbonGraph
main

搜索帮助