cobalt_generator 1.6.0
cobalt_generator: ^1.6.0 copied to clipboard
Code generator for Cobalt dependency injection. Annotate classes with @cobaltInject and build_runner writes the container, failing the build when a dependency is missing.
cobalt_generator example #
A dev_dependency — it never ships in an application.
dev_dependencies:
cobalt_generator: ^1.0.0
build_runner: ^2.15.0
dart run build_runner build
All three builders are auto_apply: dependents, so declaring the dependency is
the whole setup. From this:
@cobaltInject
class Repository {
Repository(this.database);
final Database database;
}
it writes lib/cobalt.g.dart with a named const factory class — never a
closure — and a $CobaltRootScope whose registrations are ordered by a
compile-time topological sort:
final class _RepositoryFactory implements CobaltFactory<Repository> {
const _RepositoryFactory();
@override
Repository create(CobaltResolver resolver) => Repository(resolver.get<Database>());
}
Start it with the generated $startCobalt(), or hand the pieces to
CobaltAppScope. A dependency cycle fails the build naming the cycle instead of
emitting code that would deadlock.
Full setup:
examples/codegen_basics.