目录

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.2 9.0 + 5.2 +
1.3.3 9.0 + 5.5 +
  • CarbonObjC Version Compatibility
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

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.3’

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 ```swift 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
```swift
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 ```swift 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:
```swift
Definition()
    .factory { context in 
        let fileVC = FileViewController()
        fileVC.fileManager = context[FileManagerProtocol]
        return fileVC as FileViewControllerProtocol
    }
  • Create objects with external parameters ```swift 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](/mirrors/CarbonGraph/tree/main/CarbonCore/CarbonCore/CarbonCore.docc/BasicUsage.md)

## Documentation

The documentation of this project is written in [DocC](https://developer.apple.com/documentation/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](/mirrors/CarbonGraph/tree/main/CarbonCore/CarbonCore/CarbonCore.docc/Contribution.md). You can also join the WeChat discussion group, check the QR code below.

<a href="https://weixin.qq.com/g/AQYAAMrdukzdvyo_JJrKIVjcJXhKFS2Of8v1P6K3bQIIwZ28E1BWAcsfbGWFKEHs"><img width="360" src="https://www.gitlink.org.cn/api/mirrors/CarbonGraph/raw/CarbonCore/CarbonCore/CarbonCore.docc/Resources/wechat_qr_code.jpg?ref=main"></a>&nbsp;

## License

CarbonGraph is released under the MIT license. See [LICENSE](./LICENSE) for details.
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802032778号