适用于 iOS 的 Swift 依赖注入/查找框架

徽标横幅灯
徽标横幅深色

迅捷版 
平台 
许可证 

CarbonGraph 是一个适用于 iOS 的 Swift 依赖注入/查找框架。您可以使用它在模块之间构建松散耦合。

框架 描述
碳芯 碳图的主要实现
碳对象 CarbonCore的ObjC适应框架

特征

  • 完整的依赖注入功能
  • 完整的Objective-C支持
  • 方便的对象定义DSL
  • 高性能线程安全解决方案
  • 支持解析原生 Swift 类型
  • 支持使用外部参数解析
  • 支持使用循环依赖项进行解析
  • 自动扫描配置
  • 其他模块生命周期管理功能

要求

碳图稳定版 必需的 iOS 版本 必需的 Swift 版本
1.2.2 9.0 5.2

兼容性

Xcode 版本 迅捷版 苹果操作系统版本 为分发而构建
11.4 5.2 卡特琳娜 10.15.7 通过
12.1 5.3 卡特琳娜 10.15.7 通过
12.4 5.3.2 卡特琳娜 10.15.7 通过
12.5 5.4 大苏尔 11.6 错误
12.5.1 5.4.2 大苏尔 11.6 错误
13.0 5.5 Big Sur 11.6 passing

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

Basic Usage

  • Basic object registration and resolving

let context = ApplicationContext()
context.register(builder: Definition().object(FilePath()))
appContext[FilePath.self]
  • Use configuration batch registration

class MyConfiguration: ScannableObject, Configuration {
    static func definitions() -> Definitions {
        Definition()
            .object(FilePath())
        Definition()
            .constructor(FileModel.init(filePath:))
        Definition()
          .factory { _ in FileModel(path: “/“) }
    }
}

let context = ApplicationContext()
context.register(configuration: MyConfiguration.self)
  • Multiple object definition methods

Definition().object(FilePath())
Definition().constructor(FilePath.init)
Definition().factory { _ in FilePath() }
Definition().factory(createFilePath())

static func createFilePath(context: ObjectContext) -> FilePath { FilePath() }
  • Define protocol alias for object

Definition()
    .object(FileModel(path: "/") as FileModelProtocol)
  • Define multiple protocol aliases for the object

Definition()
    .protocol(FileManagerProtocol.self)
    .alias(ImageManagerProtocol.self)
    .alias(DirectoryManagerProtocol.self)
    .object(FileManager())
  • Use constructor for dependency injection

Definition()
    .constructor(FileModel.init(filePath:))
  • Use property for dependency injection

Definition()
    .protocol(FileViewControllerProtocol.self)
    .object(FileViewController())
    .property(\.avatarFactory)
  • Use setter for dependency injection

Definition()
    .protocol(FileViewControllerProtocol.self)
    .object(FileViewController())
    .setter(FileViewController.setAvatarFactory)
  • Use static factory for dependency injection

Definition()
    .factory(fileModel(context:filePath:))
appContext[FileModelProtocol.self]

static func fileModel2(context: ObjectContext, filePath: FilePath) -> FileModelProtocol {
    FileModel(path: filePath.path, name: filePath.name)
}
  • Use a static factory for manual dependency injection

Definition()
    .factory { ctx in FileModel(filePath: ctx[FilePath.self]) as FileModelProtocol }
appContext[FileModelProtocol.self]
  • Create objects with external parameters

Definition()
    .factory(fileModel(context:arg1:arg2:))
appContext[FileModelProtocol.self, "/china/beijing", "family.png"]

static func fileModel(context: ObjectContext, arg1: String, arg2: String) -> FileModelProtocol {
    FileModel(path: arg1, name: arg2)
}

For more usage scenarios, please refer to Netdisk Demo or related unit test cases, or contact us for help.

Unit Tests

  1. Open Carbon.xcworkspace with Xcode
  2. Execute Command + U in Xcode

Discussion

Tool Address Description
Infoflow 5346856 CarbonGraph users discussion group
Email carbon-core@baidu.com CarbonGraph core contributors email group

Credits

The idea of using dependency injection to build loosely coupled projects is from Spring. The implementation of using generics to obtain method parameter types is from Swinject.

Thanks for the excellent ideas and implementation provided by the above framework.

License

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

GitHub

点击跳转