Files
cherrypick/README.md

184 lines
4.9 KiB
Markdown
Raw Normal View History

2025-05-02 12:00:06 +03:00
# CherryPick Workspace
2025-05-18 16:01:46 +03:00
CherryPick Workspace is a modular ecosystem for declarative, type-safe dependency injection in Dart and Flutter applications. It brings together core dependency management, advanced code generation, annotation-driven DI, and seamless Flutter integration for stateful, testable, and scalable app architectures.
---
2025-05-02 12:00:06 +03:00
## Overview
2025-05-18 16:01:46 +03:00
CherryPick Workspace includes the following packages:
- **`cherrypick`** Core dependency injection engine for Dart: bindings, modules, scopes, and runtime resolution.
- **`cherrypick_annotations`** Lightweight annotation library (`@module`, `@singleton`, `@named`) for injectable code and code generation.
- **`cherrypick_generator`** Code generator that produces DI module boilerplate from annotated Dart classes, using `cherrypick_annotations`.
- **`cherrypick_flutter`** Flutter integration providing scope-aware dependency resolution via `CherryPickProvider` in the widget tree.
---
2025-05-02 12:00:06 +03:00
## Repository Structure
2025-05-18 16:01:46 +03:00
- `cherrypick/` Core DI library (bindings, modules, scopes, runtime resolution)
- `cherrypick_annotations/` DI annotations for use with generators
- `cherrypick_generator/` Source-gen implementation for codegen of modules and bindings
- `cherrypick_flutter/` Flutter tools to provide DI in widget subtree via `CherryPickProvider`
- `examples/` Sample Flutter projects demonstrating patterns
---
2025-05-02 12:00:06 +03:00
## Quick Start Guide
### Installation
2025-05-18 16:01:46 +03:00
Add the desired packages to your `pubspec.yaml` (pick what you need):
2025-05-02 12:00:06 +03:00
```yaml
dependencies:
2025-05-18 16:01:46 +03:00
cherrypick: ^latest
cherrypick_annotations: ^latest
cherrypick_flutter: ^latest
dev_dependencies:
cherrypick_generator: ^latest
build_runner: ^latest
2025-05-02 12:00:06 +03:00
```
2025-05-18 16:01:46 +03:00
Run `flutter pub get` or `dart pub get` to fetch dependencies.
---
2025-05-02 12:00:06 +03:00
### Usage
2025-05-18 16:01:46 +03:00
#### Core DI (`cherrypick`)
2025-05-02 12:00:06 +03:00
2025-05-18 16:01:46 +03:00
- **Bind dependencies:**
2025-05-02 12:00:06 +03:00
```dart
Binding<String>().toInstance("hello world");
2025-05-18 16:01:46 +03:00
Binding<ApiClient>().toProvide(() => ApiClientImpl()).singleton();
2025-05-02 12:00:06 +03:00
```
2025-05-18 16:01:46 +03:00
- **Module definition:**
2025-05-02 12:00:06 +03:00
```dart
class AppModule extends Module {
@override
void builder(Scope currentScope) {
bind<ApiClient>().toInstance(ApiClientMock());
}
}
```
2025-05-18 16:01:46 +03:00
- **Scope management:**
2025-05-02 12:00:06 +03:00
```dart
2025-05-18 16:01:46 +03:00
final rootScope = CherryPick.openRootScope();
2025-05-02 12:00:06 +03:00
rootScope.installModules([AppModule()]);
2025-05-18 16:01:46 +03:00
final client = rootScope.resolve<ApiClient>();
2025-05-02 12:00:06 +03:00
```
2025-05-18 16:01:46 +03:00
You can create sub-scopes for feature isolation as needed:
```dart
final featureScope = rootScope.openSubScope("feature");
featureScope.installModules([FeatureModule()]);
```
2025-05-02 12:00:06 +03:00
2025-05-18 16:01:46 +03:00
#### Annotation & Code Generation (`cherrypick_annotations`, `cherrypick_generator`)
- **Annotate your DI modules:**
2025-05-02 12:00:06 +03:00
```dart
2025-05-18 16:01:46 +03:00
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
import 'package:cherrypick/cherrypick.dart';
2025-05-18 16:28:59 +03:00
2025-05-18 16:01:46 +03:00
part 'app_module.cherrypick.g.dart';
@module()
abstract class AppModule extends Module {
@singleton()
ApiClient client() => ApiClientImpl();
@named('apiBaseUrl')
String baseUrl() => 'https://api.example.com';
2025-05-02 12:00:06 +03:00
}
```
2025-05-18 16:01:46 +03:00
- **Generate code:**
Run:
```shell
dart run build_runner build
```
This will generate efficient registration code for your modules.
#### Flutter Integration (`cherrypick_flutter`)
- **Setup `CherryPickProvider` in your widget tree:**
2025-05-02 12:00:06 +03:00
```dart
2025-05-18 16:01:46 +03:00
void main() {
runApp(
CherryPickProvider(
child: MyApp(),
),
);
}
2025-05-02 12:00:06 +03:00
```
2025-05-18 16:01:46 +03:00
- **Access DI scopes anywhere in Flutter:**
```dart
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final cherryPick = CherryPickProvider.of(context);
final rootScope = cherryPick.openRootScope();
final repo = rootScope.resolve<MyRepository>();
// use repo as needed...
return Text('Dependency resolved!');
}
}
```
2025-05-02 12:00:06 +03:00
2025-05-18 16:01:46 +03:00
---
2025-05-02 12:00:06 +03:00
## Features
2025-05-18 16:01:46 +03:00
- [x] Module-based configuration & composition
- [x] Flexible scopes (main/root/subscopes)
- [x] Named and singleton bindings
- [x] Async binding and parameter injection
- [x] Annotations (`@module`, `@singleton`, `@named`) for concise setup
- [x] Code generation for efficient, boilerplate-free DI modules
- [x] Seamless integration with Flutter via InheritedWidget (`CherryPickProvider`)
---
## Example Projects
See the [`examples/`](examples/) directory for real-world usage patterns, including synchronous, asynchronous, and named injection in Flutter apps.
---
2025-05-02 12:00:06 +03:00
## Contributing
2025-05-18 16:01:46 +03:00
Community feedback, bug reports, and PRs are welcome! Please file issues and suggestions on the [GitHub issues page](https://github.com/pese-git/cherrypick/issues).
---
2025-05-02 12:00:06 +03:00
## License
2025-05-18 16:01:46 +03:00
CherryPick Workspace is licensed under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0).
---
2025-05-02 12:00:06 +03:00
## Links
2025-05-18 16:01:46 +03:00
- [CherryPick GitHub Repository](https://github.com/pese-git/cherrypick)
- [cherrypick_flutter on pub.dev](https://pub.dev/packages/cherrypick_flutter)
- [cherrypick_generator on pub.dev](https://pub.dev/packages/cherrypick_generator)
2025-05-18 16:28:59 +03:00
- [cherrypick_annotations on pub.dev](https://pub.dev/packages/cherrypick_annotations)