miodi 0.7.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 53

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}');
}

[0.7.1] - 09.11.2019

  • Update project description

[0.7.0] - 09.11.2019

  • Remove async injection.
  • Refactor project.

[0.6.1] - 30.01.2019

  • Change api create modules and components.
  • Fix save providers.
  • Added example dependency provide to module and providers.

[0.5.2] - 22.12.2018

  • Fix readme.

[0.5.1] - 22.12.2018

  • Added Async providers.
  • Added support for using libraries using miodi.
  • Change example.

[0.4.0] - 15.12.2018

  • Add provider name. Support for providing the same types by different names.
  • Fixed init modules.
  • Fixed create custom modules.
  • Add singleton initOnStart params.

[0.3.1] - 05.11.2018

  • Remove scope providers.

[0.3.0] - 05.11.2018

  • Changed the work of scope objects
  • Changed inject method calls
  • Added the ability to extend the functionality of the library.

[0.2.5] - 05.11.2018

  • Added more information in readme

[0.2.4] - 05.11.2018

  • Added example project

[0.2.3] - 05.11.2018

  • Hotfix get dependency in component

[0.2.2] - 03.11.2018

  • Hotfix remove default scope, add optional delay before clear scope obj (delay in progress)

[0.2.1] - 03.11.2018

  • Add default scope, add optional delay before clear scope obj

[0.2.0] - 03.11.2018

  • Add custom scopes

[0.1.2] - 01.11.2018

  • Fix lib/miodi.dart

[0.1.1] - 01.11.2018

  • Miodi beta-version added correct errors

[0.0.1] - 01.11.2018

  • Miodi beta-version

example/lib/main.dart

import 'package:example/components.dart';
import 'package:example/consts.dart';
import 'package:miodi/miodi.dart';

main(List<String> arguments) async {
  Miodi.init([AppComponent()]);
  var logger = await Miodi.inject<Logger>();
//  var logger = await injectAsync<Logger>();

  UserController controller = Miodi.inject();
//  UserController controller = inject();

  logger.log(controller.currentUser.id);
  logger.log(controller.currentUser.name);

  controller.currentUser.name = 'new_name';

  logger.log(controller.currentUser.id);
  logger.log(controller.currentUser.name);

  var currentUser = inject<User>(currentUserProvider);

  logger.log('User:: ${currentUser.id}');
  logger.log('User:: ${currentUser.name}');

  final testDependency = inject<TestDependency>();

  logger.log('TestDependency  ${testDependency.controller.currentUser.name}');
}

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()))];
}

class TestDependency {
  final UserController controller;

  TestDependency(this.controller);
}

class LoggerModule extends MiodiModule {
  static const _filePath = 'assets/file';

  @override
  List<Provider> get providers => [
    singleton(Logger, ()=> FileLogger(_filePath))
  ];
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  miodi: ^0.7.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:miodi/miodi.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
8
Health:
Code health derived from static analysis. [more]
96
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on Dec 7, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:miodi/miodi.dart.

Health issues and suggestions

Document public APIs. (-1 points)

62 out of 62 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/core/component.dart. (-1.49 points)

Analysis of lib/core/component.dart reported 3 hints:

line 31 col 7: DO use curly braces for all flow control structures.

line 33 col 7: DO use curly braces for all flow control structures.

line 35 col 7: DO use curly braces for all flow control structures.

Fix lib/miodi.dart. (-1 points)

Analysis of lib/miodi.dart reported 2 hints:

line 35 col 7: DO use curly braces for all flow control structures.

line 37 col 7: DO use curly braces for all flow control structures.

Fix lib/core/providers.dart. (-0.50 points)

Analysis of lib/core/providers.dart reported 1 hint:

line 1 col 8: Unused import: 'package:miodi/core/consts.dart'.

Fix additional 5 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/core/configuration.dart (Run dartfmt to format lib/core/configuration.dart.)
  • lib/core/consts.dart (Run dartfmt to format lib/core/consts.dart.)
  • lib/core/module.dart (Run dartfmt to format lib/core/module.dart.)
  • lib/error/errors.dart (Run dartfmt to format lib/error/errors.dart.)
  • lib/util/map_merge.dart (Run dartfmt to format lib/util/map_merge.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
meta ^1.1.6 1.1.8
Dev dependencies
test ^1.5.0