get_it_injector_gen
get_it_injector_gen
is a code generator package that complements get_it_injector
by automating the generation of code for registering classes with the get_it
package in Dart. This generator simplifies the process of dependency injection by generating the necessary code based on annotations from get_it_injector
.
Why get_it_injector_gen
?
For real-world projects, managing dependencies manually can become cumbersome. The goal of get_it_injector_gen
is to automate the registration process, making it easy to configure and manage dependencies using the get_it
package.
Features
-
Flexible Registration: Annotate your classes with
get_it_injector
annotations to define whether they should be registered as factories, singletons, or lazy singletons. The generator will handle the code generation for you. -
Order of Injection: Define class name patterns to determine the order of injection, allowing you to control the sequence in which classes are injected.
-
Grouping: Organize injectables into groups with their own priorities, providing a more fine-grained approach to dependency management.
Getting Started
-
Add the
get_it_injector
andget_it_injector_gen
packages to yourpubspec.yaml
file:dependencies: get_it_injector: # latest version dev_dependencies: get_it_injector_gen: # latest version build_runner: # latest version
-
Configure the build runner to use
get_it_injector_gen
in yourbuild.yaml
file:targets: $default: builders: get_it_injector_gen: generate_for: - lib/**/*.dart options: # Add your options here
All option's casing are formatted in snake_case
Refer to the Settings Interface file for more information on the available options.
-
Feed your classes to the generator
By Annotation
import 'package:get_it_injector/get_it_injector.dart'; @singleton class MySingletonService { // Implementation details... }
By Configuration
You can include all classes using the
auto_register
option.You can use
groups
,register
,factories
,singletons
,lazy_singletons
andpriorities
to filter the classes that should be registered. Each accept a list of regex patterns that will be used to match the class names.targets: $default: builders: get_it_injector_gen: generate_for: - lib/**/*.dart options: auto_register: true groups: - PATTERN # regex register: - PATTERN # regex factories: - PATTERN # regex singletons: - PATTERN # regex lazy_singletons: - PATTERN # regex priorities: - PATTERN # regex
For more details on each input, refer to the Settings Interface file
-
Add the setup function
import 'package:get_it/get_it.dart'; import 'package:get_it_injector/get_it_injector.dart'; import 'FILE_NAME.config.dart'; // replace FILE_NAME with the name of _this_ file final getIt = GetIt.instance; @setup Future<void> setup() { getIt.init(); }
-
Run the build command to generate the registration code:
# using flutter flutter pub run build_runner build --delete-conflicting-outputs # using dart pub run build_runner build --delete-conflicting-outputs
Issues and Contributions
If you encounter any issues or have suggestions for improvements, please feel free to open an issue or contribute to the project on GitHub.
A note about build_runner
The build runner performance can be optimized by flattening your project structure, especially if you have nested folders. Consider using the exclude
option in build.yaml
to exclude unnecessary folders from the scanning process.
targets:
$default:
builders:
get_it_injector_gen:
generate_for:
include:
- lib/**/*.dart
exclude:
- lib/ui/**
generate_for.include
& generate_for.exclude
are part of the build.yaml
format. You can find more information here
License
This software is released under the Apache 2.0 license. https://www.apache.org/licenses/LICENSE-2.0
Libraries
- builders
- Copyright 2024 CouchSurfing International Inc.
- enums/enums
- Copyright 2024 CouchSurfing International Inc.
- enums/parameter_type
- enums/register_type
- Copyright 2024 CouchSurfing International Inc.
- models/group
- Copyright 2024 CouchSurfing International Inc.
- models/implementation
- Copyright 2024 CouchSurfing International Inc.
- models/importable
- models/injectable
- Copyright 2024 CouchSurfing International Inc.
- models/models
- Copyright 2024 CouchSurfing International Inc.
- models/parameter
- Copyright 2024 CouchSurfing International Inc.
- models/parameters
- Copyright 2024 CouchSurfing International Inc.
- models/settings
- Copyright 2024 CouchSurfing International Inc.
- models/settings_interface
- Copyright 2024 CouchSurfing International Inc.