df_di 0.1.0+1 df_di: ^0.1.0+1 copied to clipboard
A powerful and versatile dependency injection (DI) system with service classes for easy state management.
DF - DI (Dependency Injection) #
Dart & Flutter Packages by DevCetra.com & contributors.
Summary #
This package offers a powerful and versatile dependency injection system along with service classes for easy state management.
Features #
- Robust support for
FutureOr
, facilitating seamless handling of both synchronous and asynchronous dependencies and callbacks. - Ability to register dependencies using both “type” and “key”, enabling the management of multiple dependencies of the same type.dependencies of the same type.
- Supports standard features like factory dependencies and lazy initialization for singleton dependencies.
- Abstract service classes that integrate effortlessly with your application.
- Clear and comprehensive documentation for easy understanding.
- Code snippets for Visual Studio Code here.
For a full feature set, please refer to the API reference.
Quickstart #
Creating a DI instance: #
// Access the global DI instance from anywhere in your app.
di;
DI.global;
// Or create a local DI instance.
final local = DI.newInstance();
Registering a Dependency: #
// Register a dependency under type "int" and defaultKey.
di.register<int>(1);
print(DIKey.defaultKey);
// Register a dependency under type "int" also but with a different key.
di.register(2, key: const DIKey('second'));
// Register a Future.
di.register<double>(Future.value(3.0));
Unregistering a Dependency: #
di.unregister<String>(); // Throws an error because there is no dependency registered under type "String".
Getting a Dependency: #
// Getting a dependency under type "int" and defaultKey.
print(di<int>()); // prints 1
// Register a dependency under type "int" also but with a different key.
print(di.get<int>(const DIKey('second'))); // prints 2
print(await di.get<double>()); // prints 3.0.
Creating a new Service: #
final class FooBarService extends Service {
// Provide Points of Data (PODs) for the UI to consume, like ValueNotifiers or Streams.
ValueListenable<String?> get vFooBar => _vFooBar;
final _vFooBar = ValueNotifier<String?>(null);
@override
FutureOr<void> onInitService() async {
_vFooBar.value = 'FooBar';
// Put initialization logic here, like starting Streams. Update the PODs as
// needed to notify the UI of changes.
}
@override
FutureOr<void> onDispose() {
_vFooBar.dispose();
// Put cleanup logic here, like disposing resources and canceling Streams.
}
}
Registering and Using a Singleton Service: #
// Register FooBarService as a lazy singleton.
di.registerSingletonService(FooBarService.new);
// Initialize the service, get it, and use it.
final fooBarService1 = await di.get<FooBarService>();
final fooBarService2 = di.get<FooBarService>();
print(fooBarService1 == fooBarService2); // prints true
print(fooBarService.vFooBar.value); // prints "FooBar"
Creating a Sync Service: #
di.registerSingletonService(SyncServiceExmple.new);
print(di.get<SyncServiceExmple>() is Future); // false
print(di.getSync<SyncServiceExmple>()); // use getSync/getSyncOrNull if you expect a sync
final class SyncServiceExmple extends Service {
@override
void onInitService() {}
@override
void onDispose() {}
}
Creating an Async Service: #
di.registerSingletonService(AsyncServiceExample.new);
print(di.get<AsyncServiceExample>() is Future); // true
print(di.getAsync<SyncServiceExmple>()); // use getAsync/getAsyncOrNull if you expect an async
final class AsyncServiceExample extends Service {
@override
Future<void> onInitService() async {
await Future<void>.delayed(
const Duration(seconds: 3),
);
}
@override
Future<void> onDispose() async {}
}
Getting the State of the DI instance: #
// Print the current state of di to understand what's registered.
print(di.registry.state);
Installation #
Use this package as a dependency by adding it to your pubspec.yaml
file (see here).
Contributing and Discussions #
This is an open-source project, and we warmly welcome contributions from everyone, regardless of experience level. Whether you're a seasoned developer or just starting out, contributing to this project is a fantastic way to learn, share your knowledge, and make a meaningful impact on the community.
Ways you can contribute: #
- Buy me a coffee: If you'd like to support the project financially, consider buying me a coffee. Your support helps cover the costs of development and keeps the project growing.
- Share your ideas: Every perspective matters, and your ideas can spark innovation.
- Report bugs: Help us identify and fix issues to make the project more robust.
- Suggest improvements or new features: Your ideas can help shape the future of the project.
- Help clarify documentation: Good documentation is key to accessibility. You can make it easier for others to get started by improving or expanding our documentation.
- Write articles: Share your knowledge by writing tutorials, guides, or blog posts about your experiences with the project. It's a great way to contribute and help others learn.
No matter how you choose to contribute, your involvement is greatly appreciated and valued!
Chief Maintainer: #
📧 Email Robert Mollentze at robmllze@gmail.com
Dontations: #
If you're enjoying this package and find it valuable, consider showing your appreciation with a small donation. Every bit helps in supporting future development. You can donate here:
https://www.buymeacoffee.com/robmllze
License #
This project is released under the MIT License. See LICENSE for more information.