cherrypick_annotations
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.
Features
- @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.
These annotations are intended to streamline DI configuration and serve as markers for code generation tools.
Getting Started
1. Add dependency
dependencies:
cherrypick_annotations: ^latest
Add as a dev_dependency for codegen:
dev_dependencies:
build_runner: ^latest
cherrypick_generator:
2. Usage
Annotate your DI modules and providers:
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
import 'package:cherrypick/cherrypick.dart';
@module()
abstract class AppModule extends Module {
@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:
final class $AppModule extends AppModule {
@override
void builder(Scope currentScope) {
bind<Dio>().toProvide(() => dio()).singleton();
bind<String>().toProvide(() => baseUrl()).withName('baseUrl');
}
}
Annotation Reference
@module
@module()
abstract class AppModule extends Module {}
Use on classes to mark them as a DI module.
@singleton
@singleton()
Dio dio() => Dio();
Use on methods or classes to provide a singleton instance.
@named
@named('token')
String token() => 'abc';
Assigns a name to the binding for keyed injection.
License
Licensed under the Apache License 2.0.
Contributing
Pull requests and feedback are welcome!
Author
Sergey Penkovsky (sergey.penkovsky@gmail.com)