jugger_generator 2.0.0 jugger_generator: ^2.0.0 copied to clipboard
Compile-time dependency injection for Dart and Flutter.
jugger.dart - Dependency Injection for Flutter and Dart #
Compile-time dependency injection for Dart and Flutter. Inspired by inject.dart and Dagger 2.
jugger:
jugger_generator:
Getting Started
In your flutter or dart project add the dependency:
dependencies:
jugger: any
dev_dependencies:
build_runner: any
dev_dependencies: any
Usage example
Define your component and module for the dependency provider, it is recommended to do this in a separate file:
// ignore_for_file: avoid_classes_with_only_static_members
import 'package:jugger/jugger.dart';
@Component(modules: <Type>[MyModule])
abstract class MyComponent {
String get helloString;
String getHelloString();
}
@module
abstract class MyModule {
@provide
static String provideString() {
return 'hello';
}
}
Now you need to execute the command
flutter packages pub run build_runner build
or for dart
dart run build_runner build --delete-conflicting-outputs
A file with the implemented component will be generated, for example main.jugger.dart
And use component as:
void main() {
final MyComponent myComponent = JuggerMyComponent.create();
print(myComponent.helloString);
print(myComponent.getHelloString());
}
SubComponents:
example:
// ignore_for_file: avoid_classes_with_only_static_members
import 'package:jugger/jugger.dart';
@Component(modules: <Type>[AppModule])
abstract class AppComponent {
int get appVersion;
}
@module
abstract class AppModule {
@provide
static int provideAppVersion() => 1;
}
@Component(
dependencies: <Type>[AppComponent],
modules: <Type>[MyModule],
)
abstract class MyComponent {
String get myHelloString;
}
@module
abstract class MyModule {
@provide
static String provideMyHelloString(int appVersion) {
return 'hello app with version: $appVersion';
}
}
@componentBuilder
abstract class MyComponentBuilder {
MyComponentBuilder appComponent(AppComponent component);
MyComponent build();
}
void main() {
final AppComponent appComponent = JuggerAppComponent.create();
final MyComponent myComponent =
JuggerMyComponentBuilder().appComponent(appComponent).build();
print(myComponent.myHelloString);
}
Unused generated providers:
Fails build if generated provider not used, by default disabled.
build.yaml
targets:
$default:
builders:
jugger_generator:
options:
check_unused_providers: true
interface prefix:
If interface have prefix 'I' you can ignore his during generation:
build.yaml
targets:
$default:
builders:
jugger_generator:
options:
remove_interface_prefix_from_component_name: true
@Component(modules: <Type>[AppModule])
abstract class IAppComponent {}
and use as:
var component = JuggerMyComponent.create();
instead:
var component = JuggerIMyComponent.create();
Bugs
If you find a bug, you can create a issue
Contributions
Contributions are welcome!
If you fixed a bug or implemented a new feature, please send a pull request.