ModuleGenerator class
CherryPick Module Generator — Codegen for DI modules
This generator scans Dart classes annotated with @module()
and generates
boilerplate for dependency injection registration automatically. Each public
method in such classes can be annotated to describe how an object should be
bound to the DI container (singleton, factory, named, with parameters, etc).
The generated code collects all such bind methods and produces a Dart
companion module registration class with a .bindAll()
method, which you
can use from your DI root to automatically register those dependencies.
Example
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
@module()
abstract class AppModule {
@singleton()
Logger logger() => Logger();
@provide()
ApiService api(Logger logger) => ApiService(logger);
@named('dev')
FakeService fake() => FakeService();
}
After codegen, you will get (simplified):
class _\$AppModuleCherrypickModule extend AppModule {
static void bindAll(CherryPickScope scope, AppModule module) {
scope.addSingleton<Logger>(() => module.logger());
scope.addFactory<ApiService>(() => module.api(scope.resolve<Logger>()));
scope.addFactory<FakeService>(() => module.fake(), named: 'dev');
}
}
Use it e.g. in your bootstrap:
final scope = CherryPick.openRootScope()..intallModules([_\$AppModuleCherrypickModule()]);
Features supported:
- Singleton, factory, named, parametric, and async providers
- Eliminates all boilerplate for DI registration
- Works with abstract classes and real classes
- Error if @module() is applied to a non-class
See also: @singleton
, @provide
, @named
, @module
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- throwOnUnresolved → bool
-
finalinherited
- typeChecker → TypeChecker
-
no setterinherited
Methods
-
generate(
LibraryReader library, BuildStep buildStep) → FutureOr< String> -
Generates Dart code for an input Dart library.
inherited
-
generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) → String -
Generates Dart source for a class marked with the
@module()
annotation. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited