https://github.com/exeteres/dino
A Dart dependency injection library aimed to be flexible, predictable and easy to use.
https://github.com/exeteres/dino
codegeneration dart dependency-injection flutter library
Last synced: 19 days ago
JSON representation
A Dart dependency injection library aimed to be flexible, predictable and easy to use.
- Host: GitHub
- URL: https://github.com/exeteres/dino
- Owner: Exeteres
- License: mit
- Created: 2022-07-22T09:16:45.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-10-18T12:26:15.000Z (about 1 year ago)
- Last Synced: 2025-01-17T10:11:51.624Z (9 months ago)
- Topics: codegeneration, dart, dependency-injection, flutter, library
- Language: Dart
- Homepage:
- Size: 148 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Dino is a Dart dependency injection library with optional code generation.
It was inspired by [DI in .NET](https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection) and aimed to be flexible, predictable and easy to use.### Quick start
> It is assumed that you have a basic understanding of [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection).
Suppose we have multiple services dependent on each other:
```dart
class Repository {
Repository(this.restClient);
final RestClient restClient;Future sendMessage(String message) async {
try {
await restClient.sendMessage(message);
} catch (e) {
print('Error sending message: $e');
}
}
}class RestClient {
RestClient(this.dio);
final Dio dio;Future sendMessage(String message) async {
await dio.post('/api/message', data: {'message': message});
}
}
```Then their registration in dino will look like this:
```dart
void main() {
final services = ServiceCollection();services.addInstance(Dio());
services.addSingletonFactory(
(sp) => RestClient(
sp.getRequired(),
),
);services.addSingletonFactory(
(sp) => Repository(
sp.getRequired(),
),
);
}
```If we add code generation using dino_generator, then the code will become even nicer:
```dart
import 'main.dino.g.dart';void main() {
final services = ServiceCollection();services.addInstance(Dio());
services.addRestClient();
services.addRepository();
}
```Now we can use registered services:
```dart
final rootScope = services.buildRootScope();final repository = rootScope.serviceProvider.getRequired();
repository.sendMessage('Hello world!');
```You can also use dino in flutter with `dino_flutter` package:
```dart
void main() {
final services = ServiceCollection();services.addInstance(Dio());
services.addRestClient();
services.addRepository();final rootScope = services.buildRootScope();
runApp(
DinoProvider(
serviceProvider: scope.serviceProvider,
child: Application(),
),
);
}
```Then you can use the `ServiceProvider` from the `BuildContext`:
```dart
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final repository = context.sp.getRequired();// build widget
}
}
```For a better understanding of concepts such as `ServiceCollection`, `ServiceScope`, and `ServiceProvider`, and to learn more about dino, you can check out the detailed documentation.
### Documentation
- [Core library](https://github.com/Exeteres/dino/blob/master/docs/core-library.md)
- [Code generation](https://github.com/Exeteres/dino/blob/master/docs/code-generation.md)### Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
### License
[MIT](https://choosealicense.com/licenses/mit/)