miodi 0.5.1 miodi: ^0.5.1 copied to clipboard
Dependency Injection tools for Dart.
miodi #
Dependency Injection tools for Dart.
This library aims to easily manage dependencies in your business logic components.
The project is in beta testing, please write about all the errors found.
https://pub.dartlang.org/packages/miodi
Docs #
factory
and asyncFactory
each time you call inject
or injectAsync
create a new instance.
singleton
and asyncSingleton
returns a single object instance, also guarantees that the object will be initialized only once.
component
function to create MiodiComponent, you can also inherit from the class MiodiComponent if you want to expand its functionality.
module
function to create MiodiModule, you can also inherit from the class MiodiModule if you want to expand its functionality
Example #
Added example project. Pls check for future updates for more information. https://github.com/Miolin/miodi/tree/master/example
You can make an injection into the constructors of your objects, there are no restrictions.
Create component:
class AppComponent extends MiodiComponent {
AppComponent() : super([controllerModule, appModule, loggerModule]);
}
final controllerModule =
module(providers: [factory(UserController, () => UserController())]);
final appModule = module(
providers: [
singleton(User, () => User('0', 'currentUser'),
name: currentUserProvider)
],
);
final loggerModule = LoggerModule();
class LoggerModule extends MiodiModule {
static const _filePath = 'assets/file';
LoggerModule()
: super(asyncProviders: [
asyncSingleton(Logger, () async {
var file = await loadFile(_filePath);
return FileLogger(file);
}, initOnStart: true)
]);
}
In the body of the component, use the get()
method to get dependencies (from the current or other modules).
You can use several components.
You can also use the component's constructor to represent dependencies in modules.
Usage example:
void main() async {
Miodi.init([appComponent]);
Database database = Miodi.inject();
// or
User user = inject();
LogSender sender = Miodi.inject();
sender.sendLog();
}