Skip to content

Commit

Permalink
Update .gitignore, LICENSE and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofei86 committed Jan 26, 2022
1 parent 22b0f8e commit 9a2b9b9
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 6 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ playground.xcworkspace
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#

Pods/

# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

Expand Down Expand Up @@ -88,3 +88,6 @@ fastlane/test_output
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

# MacOS
.DS_Store
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Baidu
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
Expand Down
175 changes: 173 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,173 @@
# CarbonGraph
A Swift dependency injection / lookup framework for iOS
![Banner](./Images/carbon_banner_light.png)

![Swift Version](https://img.shields.io/badge/Swift-5.1--5.5-orange?style=flat-square) 
![Platforms](https://img.shields.io/badge/platform-ios-lightgray?style=flat-square) 
![License](https://img.shields.io/badge/license-MIT-black.svg?style=flat-square) 

CarbonGraph is a Swift dependency injection / lookup framework for iOS. You can use it to build loose coupling between modules.

| Framework | Description |
| --- | --- |
| CarbonCore | The main implementation of CarbonGraph |
| CarbonObjC | CarbonCore's ObjC adaptation framework |

## Features

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

## Requirements

| CarbonGraph Stable Version | Required iOS Version | Required Swift Version |
| --- | --- | --- |
| 1.1.0 | 9.0 | 5.2 |

### Compatibility

| Xcode Version | Swift Version | MacOS Version | Build for distribution |
| --- | --- | --- | --- |
| 11.4 | 5.2 | Catalina 10.15.7 | passing |
| 12.1 | 5.3 | Catalina 10.15.7 | passing |
| 12.4 ☑ | 5.3.2 | Catalina 10.15.7 | passing |
| ~~12.5~~ | ~~5.4~~ | ~~Big Sur 11.6~~ | ~~error~~ |
| ~~12.5.1~~ | ~~5.4.2~~ | ~~Big Sur 11.6~~ | ~~error~~ |
| 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
```swift
let context = ApplicationContext()
context.register(builder: Definition().object(FilePath()))
appContext[FilePath.self]
```

* Use configuration batch registration
```swift
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
```swift
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
```swift
Definition()
.object(FileModel(path: "/") as FileModelProtocol)
```

* Define multiple protocol aliases for the object
```swift
Definition()
.protocol(FileManagerProtocol.self)
.alias(ImageManagerProtocol.self)
.alias(DirectoryManagerProtocol.self)
.object(FileManager())
```

* Use constructor for dependency injection
```swift
Definition()
.constructor(FileModel.init(filePath:))
```

* Use property for dependency injection
```swift
Definition()
.protocol(FileViewControllerProtocol.self)
.object(FileViewController())
.property(\.avatarFactory)
```

* Use setter for dependency injection
```swift
Definition()
.protocol(FileViewControllerProtocol.self)
.object(FileViewController())
.setter(FileViewController.setAvatarFactory)
```

* Use static factory for dependency injection
```swift
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
```swift
Definition()
.factory { ctx in FileModel(filePath: ctx[FilePath.self]) as FileModelProtocol }
appContext[FileModelProtocol.self]
```

* Create objects with external parameters
```swift
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 | [email protected] | CarbonGraph core contributors email group |

## Credits

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).

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

## License

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 comments on commit 9a2b9b9

Please sign in to comment.