|
1 |
| -# CarbonGraph |
2 |
| -A Swift dependency injection / lookup framework for iOS |
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +CarbonGraph is a Swift dependency injection / lookup framework for iOS. You can use it to build loose coupling between modules. |
| 8 | + |
| 9 | +| Framework | Description | |
| 10 | +| --- | --- | |
| 11 | +| CarbonCore | The main implementation of CarbonGraph | |
| 12 | +| CarbonObjC | CarbonCore's ObjC adaptation framework | |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +- [x] Complete dependency injection capabilities |
| 17 | +- [x] Complete Objective-C support |
| 18 | +- [x] Convenient object definition DSL |
| 19 | +- [x] High-performance thread-safe solution |
| 20 | +- [x] Support resolving native Swift types |
| 21 | +- [x] Support resolving with external parameters |
| 22 | +- [x] Support resolving with circular dependencies |
| 23 | +- [x] Automatic scanning of configuration |
| 24 | +- [x] Additional module life cycle management capabilities |
| 25 | + |
| 26 | +## Requirements |
| 27 | + |
| 28 | +| CarbonGraph Stable Version | Required iOS Version | Required Swift Version | |
| 29 | +| --- | --- | --- | |
| 30 | +| 1.1.0 | 9.0 | 5.2 | |
| 31 | + |
| 32 | +### Compatibility |
| 33 | + |
| 34 | +| Xcode Version | Swift Version | MacOS Version | Build for distribution | |
| 35 | +| --- | --- | --- | --- | |
| 36 | +| 11.4 | 5.2 | Catalina 10.15.7 | passing | |
| 37 | +| 12.1 | 5.3 | Catalina 10.15.7 | passing | |
| 38 | +| 12.4 ☑ | 5.3.2 | Catalina 10.15.7 | passing | |
| 39 | +| ~~12.5~~ | ~~5.4~~ | ~~Big Sur 11.6~~ | ~~error~~ | |
| 40 | +| ~~12.5.1~~ | ~~5.4.2~~ | ~~Big Sur 11.6~~ | ~~error~~ | |
| 41 | +| 13.0 ☑ | 5.5 | Big Sur 11.6 | passing | |
| 42 | + |
| 43 | +## Installation |
| 44 | + |
| 45 | +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: |
| 46 | + |
| 47 | +pod 'CarbonCore', '~> 1.2.2' |
| 48 | + |
| 49 | +## Basic Usage |
| 50 | + |
| 51 | +* Basic object registration and resolving |
| 52 | +```swift |
| 53 | +let context = ApplicationContext() |
| 54 | +context.register(builder: Definition().object(FilePath())) |
| 55 | +appContext[FilePath.self] |
| 56 | +``` |
| 57 | + |
| 58 | +* Use configuration batch registration |
| 59 | +```swift |
| 60 | +class MyConfiguration: ScannableObject, Configuration { |
| 61 | + static func definitions() -> Definitions { |
| 62 | + Definition() |
| 63 | + .object(FilePath()) |
| 64 | + Definition() |
| 65 | + .constructor(FileModel.init(filePath:)) |
| 66 | + Definition() |
| 67 | + .factory { _ in FileModel(path: “/“) } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +let context = ApplicationContext() |
| 72 | +context.register(configuration: MyConfiguration.self) |
| 73 | +``` |
| 74 | + |
| 75 | +* Multiple object definition methods |
| 76 | +```swift |
| 77 | +Definition().object(FilePath()) |
| 78 | +Definition().constructor(FilePath.init) |
| 79 | +Definition().factory { _ in FilePath() } |
| 80 | +Definition().factory(createFilePath()) |
| 81 | + |
| 82 | +static func createFilePath(context: ObjectContext) -> FilePath { FilePath() } |
| 83 | +``` |
| 84 | + |
| 85 | +* Define protocol alias for object |
| 86 | +```swift |
| 87 | +Definition() |
| 88 | + .object(FileModel(path: "/") as FileModelProtocol) |
| 89 | +``` |
| 90 | + |
| 91 | +* Define multiple protocol aliases for the object |
| 92 | +```swift |
| 93 | +Definition() |
| 94 | + .protocol(FileManagerProtocol.self) |
| 95 | + .alias(ImageManagerProtocol.self) |
| 96 | + .alias(DirectoryManagerProtocol.self) |
| 97 | + .object(FileManager()) |
| 98 | +``` |
| 99 | + |
| 100 | +* Use constructor for dependency injection |
| 101 | +```swift |
| 102 | +Definition() |
| 103 | + .constructor(FileModel.init(filePath:)) |
| 104 | +``` |
| 105 | + |
| 106 | +* Use property for dependency injection |
| 107 | +```swift |
| 108 | +Definition() |
| 109 | + .protocol(FileViewControllerProtocol.self) |
| 110 | + .object(FileViewController()) |
| 111 | + .property(\.avatarFactory) |
| 112 | +``` |
| 113 | + |
| 114 | +* Use setter for dependency injection |
| 115 | +```swift |
| 116 | +Definition() |
| 117 | + .protocol(FileViewControllerProtocol.self) |
| 118 | + .object(FileViewController()) |
| 119 | + .setter(FileViewController.setAvatarFactory) |
| 120 | +``` |
| 121 | + |
| 122 | +* Use static factory for dependency injection |
| 123 | +```swift |
| 124 | +Definition() |
| 125 | + .factory(fileModel(context:filePath:)) |
| 126 | +appContext[FileModelProtocol.self] |
| 127 | + |
| 128 | +static func fileModel2(context: ObjectContext, filePath: FilePath) -> FileModelProtocol { |
| 129 | + FileModel(path: filePath.path, name: filePath.name) |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +* Use a static factory for manual dependency injection |
| 134 | +```swift |
| 135 | +Definition() |
| 136 | + .factory { ctx in FileModel(filePath: ctx[FilePath.self]) as FileModelProtocol } |
| 137 | +appContext[FileModelProtocol.self] |
| 138 | +``` |
| 139 | + |
| 140 | +* Create objects with external parameters |
| 141 | +```swift |
| 142 | +Definition() |
| 143 | + .factory(fileModel(context:arg1:arg2:)) |
| 144 | +appContext[FileModelProtocol.self, "/china/beijing", "family.png"] |
| 145 | + |
| 146 | +static func fileModel(context: ObjectContext, arg1: String, arg2: String) -> FileModelProtocol { |
| 147 | + FileModel(path: arg1, name: arg2) |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +For more usage scenarios, please refer to Netdisk Demo or related unit test cases, or contact us for help. |
| 152 | + |
| 153 | +## Unit Tests |
| 154 | + |
| 155 | +1. Open Carbon.xcworkspace with Xcode |
| 156 | +2. Execute Command + U in Xcode |
| 157 | + |
| 158 | +## Discussion |
| 159 | + |
| 160 | +| Tool | Address | Description | |
| 161 | +| --- | --- | --- | |
| 162 | +| Infoflow | 5346856 | CarbonGraph users discussion group | |
| 163 | +| Email | [email protected] | CarbonGraph core contributors email group | |
| 164 | + |
| 165 | +## Credits |
| 166 | + |
| 167 | +The idea of using dependency injection to build loosely coupled projects is from [Spring](https://spring.io). The implementation of using generics to obtain method parameter types is from [Swinject](https://github.com/Swinject/Swinject). |
| 168 | + |
| 169 | +Thanks for the excellent ideas and implementation provided by the above framework. |
| 170 | + |
| 171 | +## License |
| 172 | + |
| 173 | +CarbonGraph is released under the MIT license. See [LICENSE](https://console.cloud.baidu-int.com/devops/icode/repos/baidu/netdisk/ios-carbon-lib/blob/master:LICENSE) for details. |
0 commit comments