Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/flutterando/auto_injector

Dependency injection system. But without build_runner :)
https://github.com/flutterando/auto_injector

dart flutter flutterando

Last synced: about 11 hours ago
JSON representation

Dependency injection system. But without build_runner :)

Awesome Lists containing this project

README

        

Auto Injector - Automatic Dependency injection system without build_runner.






Logo


A simple way to inject dependencies in your project.



Explore the docs »







Report Bug
·
Request Feature


[![Version](https://img.shields.io/github/v/release/flutterando/auto_injector?style=plastic)](https://pub.dev/packages/auto_injector)
[![Pub Points](https://img.shields.io/pub/points/auto_injector?label=pub%20points&style=plastic)](https://pub.dev/packages/auto_injector/score)
[![Flutterando Analysis](https://img.shields.io/badge/style-flutterando__analysis-blueviolet?style=plastic)](https://pub.dev/packages/flutterando_analysis/)

[![Pub Publisher](https://img.shields.io/pub/publisher/auto_injector?style=plastic)](https://pub.dev/publishers/flutterando.com.br/packages)


---

Table of Contents


  1. About The Project

  2. Sponsors

  3. Getting Started

  4. How to Use


    1. Dispose Singleton

    2. Modularization

    3. Param Transform


  5. Features

  6. Contributing

  7. Contact

  8. Acknowledgements

---


## About The Project
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.

(back to top)

## Sponsors


Logo

(back to top)



## Getting Started

To get Auto Injector in your project follow either of the instructions below:

a) Add your_package as a dependency in your Pubspec.yaml:
```yaml
dependencies:
auto_injector: ^1.0.2
```

b) Use Dart Pub:
```sh
dart pub add auto_injector
```


## How to Use

Register instances:

```dart
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 {}

```

Register instances with Key:

```dart

autoInjector.add(Controller.new, key: 'MyCustomName');

```

Get instance:

```dart
// fetch
final controller = autoInjector.get();
print(controller); // Instance of 'Controller'.

// or use calleble function (withless .get())
final datasource = autoInjector();
print(datasource); // Instance of 'Datasource'.
```

Get instance by key

```dart
// fetch
final controller = autoInjector.get(key: 'CustomController');
print(controller); // Instance of 'Controller'.
```

Try get instance:

```dart
// use tryGet that returns null if exception.
final datasource = autoInjector.tryGet() ?? 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.

```dart
final datasource = autoInjector.get(transform: changeParam(DataSourceMock()));
print(datasource); // Instance of 'Datasource'.
```

###

Dispose Singleton

Singletons can be terminated on request using the `disposeSingleton` method returning
the instance for executing the dispose routine.

```dart

final deadInstance = autoInjector.disposeSingleton();
deadInstance.close();

```

###

Modularization

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.

```dart

// 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());
print(appModule.get());
}

```

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:

```dart
autoInjector.disposeSingletonsByTag('ProductModule', (instance){
// individual dispose routine
});
```

###

Param Transform

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:

```dart
final homeModule = AutoInjector(
paramTransforms: [
(param) {
if(param is NamedParam){
return param;
} else if(param is PositionalParam) {
return param;
}
],
);

```



###

BindConfig

If there is a need to configure the dispose and notifier of the bind,
use the `BindConfig` property.
This is very useful if you want to automate class disposes like BLoC or Triple Store:

```dart
final injector = AutoInjector();

final config = BindConfig(
onDispose: (bloc) => bloc.close(),
onNotifier: (bloc) => bloc.stream,
);

injector.addSingleton(ProductBloc.new, config: config);

```


_For more examples, please refer to the_ [Documentation](https://pub.dev/documentation/auto_injector/latest/)

(back to top)

## Features

- ✅ 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.

(back to top)

## Contributing

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!

1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

Remember to include a tag, and to follow [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) and [Semantic Versioning](https://semver.org/) when uploading your commit and/or creating the issue.

(back to top)

## Contact

Flutterando Community
- [Discord](https://discord.gg/qNBDHNARja)
- [Telegram](https://t.me/flutterando)
- [Website](https://www.flutterando.com.br)
- [Youtube Channel](https://www.youtube.com.br/flutterando)
- [Other useful links](https://linktr.ee/flutterando)

(back to top)

## Acknowledgements

Thank you to all the people who contributed to this project, whithout you this project would not be here today.




(back to top)

## Maintaned by








Built and maintained by Flutterando.