miodi 0.7.1 miodi: ^0.7.1 copied to clipboard
Dependency Injection tools for Dart. This library aims to easily manage dependencies in your business logic components.
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
each time returns a new instance to an injection. -
singleton
returns a single object instance, also guarantees that the object will be initialized only once. -
MiodiComponent
component containing modules and providing dependencies for modules. -
module
function to create MiodiModule, you can also inherit from the class MiodiModule if you want to expand its functionality. module contains a list of providers.
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 {
@override
List<MiodiModule> get modules => [
module(
providers: [
factory(
UserController,
() => UserController(get(name: currentUserProvider)),
),
],
),
appModule,
loggerModule,
ExampleDependencyModule(get)
];
}
final appModule = module(
providers: [
singleton(User, () => User('0', 'currentUser'), name: currentUserProvider)
],
);
final loggerModule = LoggerModule();
typedef T DependencyProvider<T>();
class ExampleDependencyModule extends MiodiModule {
final DependencyProvider<UserController> controllerProvider;
ExampleDependencyModule(this.controllerProvider);
@override
List<Provider> get providers =>
[factory(TestDependency, () => TestDependency(controllerProvider()))];
}
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()]);
final logger = await Miodi.inject<Logger>();
//or
UserController controller = inject();
final testDependency = inject<TestDependency>();
logger.log('TestDependency ${testDependency.controller.currentUser.name}');
}