![Logo](https://raw.githubusercontent.com/Flutterando/auto_injector/main/readme_assets/logo-auto-injection.png)
A simple way to inject dependencies in your project.
Explore the docs »
Report Bug
·
Request Feature
Table of Contents
Auto Injector is a Dart package that was created to make the developer's life easier, making the method of how to do dependency injection simpler and more practical. Just create the class that will be injected and declare it within some of the available "add" methods and that's it, you already have your dependency injection ready to use.
This project is distributed under the MIT License. See LICENSE.txt
for more information.
![Logo](https://raw.githubusercontent.com/Flutterando/README-Template/master/readme_assets/sponsor-logo.png)
To get Auto Injector in your project follow either of the instructions below:
a) Add your_package as a dependency in your Pubspec.yaml:
dependencies:
auto_injector: ^1.0.2
b) Use Dart Pub:
dart pub add auto_injector
Register instances:
final autoInjector = AutoInjector();
void main(){
// factory
autoInjector.add(Controller.new);
// Singleton
autoInjector.addSingleton(Datasource.new);
// lazySingleton
autoInjector.addLazySingleton(Repository.new);
// instance
autoInjector.instance('Instance');
// Inform that you have finished adding instances
autoInjector.commit();
}
class Controller {
final Repository repository;
Controller(this.repository);
}
class Repository {
final Datasource datasource;
Repository({required this.datasource});
}
class Datasource {}
Get instance:
// fetch
final controller = autoInjector.get<Controller>();
print(controller); // Instance of 'Controller'.
// or use calleble function (withless .get())
final datasource = autoInjector<Datasource>();
print(datasource); // Instance of 'Datasource'.
Try get instance:
// use tryGet that returns null if exception.
final datasource = autoInjector.tryGet<Datasource>() ?? Datasource();
print(datasource); // Instance of 'Datasource'.
Get instance and transform params. This can be used for example to replace an instance with a mock in tests.
final datasource = autoInjector.get<Datasource>(transform: changeParam(DataSourceMock()));
print(datasource); // Instance of 'Datasource'.
Singletons can be terminated on request using the disposeSingleton
method returning
the instance for executing the dispose routine.
final deadInstance = autoInjector.disposeSingleton<MyController>();
deadInstance.close();
For projects with multiple scopes, try uniting the instances by naming them Module or Container. With this, you can register specific instances for each module.
// app_module.dart
final appModule = AutoInjector(
tag: 'AppModule',
on: (i) {
i.addInjector(productModule);
i.addInjector(userModule);
i.commit();
},
);
...
// product_module.dart
final productModule = AutoInjector(
tag: 'ProductModule',
on: (i) {
i.addInstance(1);
},
);
...
// user_module.dart
final userModule = AutoInjector(
tag: 'UserModule',
on: (i) {
i.addInstance(true);
},
);
...
void main() {
print(appModule.get<int>());
print(appModule.get<bool>());
}
It is also possible to remove all singletons from a specific tag using the method
disposeSingletonsByTag
which reports each instance removed via an anonymous function:
autoInjector.disposeSingletonsByTag('ProductModule', (instance){
// individual dispose routine
});
There is the possibility to listen and transform all the parameters that are being analyzed
when there is an instance request (AutoInject.get()
). Add transformers on the main instance:
final homeModule = AutoInjector(
paramTransforms: [
(param) {
if(param is NamedParam){
return param;
} else if(param is PositionalParam) {
return param;
}
],
);
###
If there is a need to configure the dispose and notifier of the bind,
use the BindConfig<T>
property.
This is very useful if you want to automate class disposes like BLoC or Triple Store:
final injector = AutoInjector();
final config = BindConfig<Bloc>(
onDispose: (bloc) => bloc.close(),
onNotifier: (bloc) => bloc.stream,
);
injector.addSingleton(ProductBloc.new, config: config);
For more examples, please refer to the Documentation
- ✅ Auto Dependency Injection
- ✅ Factory Injection
- ✅ Singleton Injection
- ✅ Lazy Singleton Injection
- ✅ Instance Injection
Right now this package has concluded all his intended features. If you have any suggestions or find something to report, see below how to contribute to it.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the appropriate tag. Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Remember to include a tag, and to follow Conventional Commits and Semantic Versioning when uploading your commit and/or creating the issue.
Flutterando Community
Thank you to all the people who contributed to this project, whithout you this project would not be here today.
Built and maintained by Flutterando.