0 purchases
dino extensions
Dino is a Dart dependency injection library with optional code generation.
It was inspired by DI in .NET and aimed to be flexible, predictable and easy to use.
Quick start #
It is assumed that you have a basic understanding of dependency injection.
Suppose we have multiple services dependent on each other:
class Repository {
Repository(this.restClient);
final RestClient restClient;
Future<void> 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<void> sendMessage(String message) async {
await dio.post('/api/message', data: {'message': message});
}
}
copied to clipboard
Then their registration in dino will look like this:
void main() {
final ServiceCollection services = RuntimeServiceCollection();
services.addInstance(Dio());
services.addSingletonFactory(
(sp) => RestClient(
dio: sp.getRequired<Dio>(),
),
);
services.addSingletonFactory(
(sp) => Repository(
restClient: sp.getRequired<RestClient>(),
),
);
}
copied to clipboard
If we add code generation using dino_generator, then the code will become even nicer:
import 'main.dino.g.dart';
void main() {
final ServiceCollection services = $ServiceCollection();
services.addInstance(Dio());
services.addSingleton<RestClient>();
services.addSingleton<Repository>();
}
copied to clipboard
Now we can use registered services:
final rootScope = services.buildRootScope();
final repository = rootScope.serviceProvider.getRequired<Repository>();
repository.sendMessage('Hello world!');
copied to clipboard
You can also use dino in flutter with dino_flutter package:
void main() {
final ServiceCollection services = $ServiceCollection();
services.addInstance(Dio());
services.addSingleton<RestClient>();
services.addSingleton<Repository>();
final rootScope = services.buildRootScope();
runApp(
DinoProvider(
serviceProvider: scope.serviceProvider,
child: Application(),
),
);
}
copied to clipboard
Then you can use the ServiceProvider from the BuildContext:
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final repository = context.sp.getRequired<Repository>();
// build widget
}
}
copied to clipboard
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
Code generation
Extensions
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
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.