Files
cherrypick/cherrypick_annotations/README.md

115 lines
2.2 KiB
Markdown
Raw Normal View History

2025-05-18 14:01:00 +03:00
# cherrypick_annotations
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
A lightweight set of Dart annotations designed for dependency injection (DI) frameworks and code generation, inspired by modern approaches like Dagger and Injectable. Works best in tandem with [`cherrypick_generator`](https://pub.dev/packages/cherrypick_generator).
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
---
2025-05-14 12:53:51 +03:00
## Features
2025-05-18 14:01:00 +03:00
- **@module** Marks a class as a DI module for service/provider registration.
- **@singleton** Declares that a method or class should be provided as a singleton.
- **@named** Assigns a string name to a binding for keyed resolution.
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
These annotations are intended to streamline DI configuration and serve as markers for code generation tools.
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
---
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
## Getting Started
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
### 1. Add dependency
```yaml
dependencies:
cherrypick_annotations: ^latest
```
Add as a `dev_dependency` for codegen:
```yaml
dev_dependencies:
build_runner: ^latest
cherrypick_generator:
```
### 2. Usage
Annotate your DI modules and providers:
```dart
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
2025-05-18 16:01:46 +03:00
import 'package:cherrypick/cherrypick.dart';
2025-05-18 14:01:00 +03:00
@module()
2025-05-18 16:01:46 +03:00
abstract class AppModule extends Module {
2025-05-18 14:01:00 +03:00
@singleton()
Dio dio() => Dio();
@named('baseUrl')
String baseUrl() => 'https://api.example.com';
}
```
When used with `cherrypick_generator`, code similar to the following will be generated:
```dart
final class $AppModule extends AppModule {
@override
void builder(Scope currentScope) {
bind<Dio>().toProvide(() => dio()).singleton();
bind<String>().toProvide(() => baseUrl()).withName('baseUrl');
}
}
```
---
## Annotation Reference
### `@module`
```dart
@module()
2025-05-18 16:01:46 +03:00
abstract class AppModule extends Module {}
2025-05-18 14:01:00 +03:00
```
Use on classes to mark them as a DI module.
---
### `@singleton`
```dart
@singleton()
Dio dio() => Dio();
```
Use on methods or classes to provide a singleton instance.
---
### `@named`
2025-05-14 12:53:51 +03:00
```dart
2025-05-18 14:01:00 +03:00
@named('token')
String token() => 'abc';
2025-05-14 12:53:51 +03:00
```
2025-05-18 14:01:00 +03:00
Assigns a name to the binding for keyed injection.
---
## License
Licensed under the [Apache License 2.0](LICENSE).
---
## Contributing
Pull requests and feedback are welcome!
---
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
## Author
2025-05-14 12:53:51 +03:00
2025-05-18 14:01:00 +03:00
Sergey Penkovsky (<sergey.penkovsky@gmail.com>)