Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kmartins/tree_man
Package for dependency injection
https://github.com/kmartins/tree_man
Last synced: 1 day ago
JSON representation
Package for dependency injection
- Host: GitHub
- URL: https://github.com/kmartins/tree_man
- Owner: kmartins
- Created: 2024-11-23T16:50:12.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-12-05T18:14:34.000Z (about 2 months ago)
- Last Synced: 2024-12-05T19:23:37.621Z (about 2 months ago)
- Language: Dart
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Tree man
This package helps you to manage any dependencies in your project without need to use context and makes possible to create dependencies for modules. The objective is help any developer to manage dependencies with a easy to use API.
It's basically the copy of this package [Flutter Injections](https://github.com/gabuldev/flutter_injections).
## Why Tree Man?
- __Fast and Efficient__
> Flutter Injections use search-tree to get the dependencies, this improve the speed to get them, and use less CPU to search for specific objects.
- __Module Injections__
> Create module injections that have all dependencies needed on your widgets. I.E __`HomeModule`__ have all dependencies needed on __`HomePage`__.
- __Easy to use__
> The focus is to keep it simple to handle dependencies on large scale applications.
- __Auto dispose__
> Objects are auto disposed when the `module` is removed from the Widget Tree.## How to use
It`s simple, just three steps:
1. Add the TreeMan to __pubspec.yaml__ file
```yaml
tree_man: ^any # or current version
```
2. Create a __Module__
```dart
class MainClass {
final String name = 'name';
}class MainModule extends Module {
@override
List> get injections => [
Inject.singleton(
(_) => MainClass(),
),
];
}
```
3. Create your __DepsModule__ and pass the `module`
```dart
MaterialApp(
home: FlutterModule(
createModule: MainModule.new,
builder: (_) => const Text('Teste'),
loading: const Text('loading'),
),
),
```4. And finally, use it to get the dependencies:
```dart
final controller = Deps.get();
```### Async Injections
You can use the __`Inject.asyncSingleton`__ to create async injections.
For know if the module is ready you can use the __`Deps.isModuleReady`__ method and for waiting the async module to be ready you can use the __`Deps.waitAsyncModuleIsReady`__ method.
The `FlutterModule` widget is already in control of this for default and has a __`loading`__ parameter that is used when the module is not ready, if null then the `CircularProgressIndicator` is used.
```dart
MaterialApp(
home: FlutterModule(
createModule: AsyncMainModule.new,
builder: (_) => const Text('Teste'),
loading: const Text('loading'),
),
),
````## Dispose
When you use the `FlutterModule` widget, the dependencies of the module are disposed of when the widget is removed from the tree and the **dispose** method of `Injection` is called.
```dart
Inject.singleton(
(_) => DisposeClass(),
dispose: (instance) => instance.dispose(),
),
```## Module
Called the `Deps.addModule` for added a module e for remove you must call `Deps.removeModule` that will dispose all injections in the module automatically.
The `FlutterModule` widget calls these methods by default.
## Test
You can override the dependencies using the __`overrideInstance`__ method.
```dart
Deps.overrideInstance(YourRepositoryMock());
```then
```dart
Deps.get() will return `YourRepositoryMock`.
```## 📝 Maintainers
[Kauê Martins](https://github.com/kmartins)
## 🤝 Support
You liked this package? Then give it a ⭐️. If you want to help then:
- Fork this repository
- Send a Pull Request with new features
- Share this package
- Create issues if you find a bug or want to suggest a new extension**Pull Request title follows [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/). **
## 📝 License
Copyright © 2024 [Kauê Martins](https://github.com/kmartins).
This project is [MIT](https://opensource.org/licenses/MIT) licensed.