miodi

Dependency Injection tools for Dart.

https://pub.dartlang.org/packages/miodi

Docs and Example project

Example project is under development.

Docs are pending.

Example

Create component:


class AppComponent extends MiodiComponent {
  static String testScope = 'test';
  final String param;

  AppComponent(this.param);

  @override
  List<MiodiModule> get modules => [
        module([
          singleton(Database, () => DatabaseImpl()),
          factory(User, () => User())
        ]),
        module([
          singleton(Logger, () => FileLogger(param)),
          factory(LogSender, () => LogSender(get())),
          scope(testScope, ScopeObject, () => ScopeObject())
        ])
      ];
}

class ScopeObject {}

abstract class Database {}

class DatabaseImpl implements Database {}

class User {}

abstract class Logger {}

class FileLogger implements Logger {
  final String tag;
  FileLogger(this.tag);
}

class LogSender {
  final Logger logger;
  LogSender(this.logger);
}

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:

final appComponent = AppComponent('TAG');

void main() async {
  Miodi.init([appComponent]);

  Database database = inject();
  //if you have a lot of big components override method 'String get componentNamespace' in your components
  //this will speed up the search for dependencies
  User user = inject('customComponentNamespace');
  
  
  ScopeObject scopeObject = scopeInject(AppComponent.testScope);
  
  //For cleaning of scopes it is NECESSARY!! use cleaning methods.
  //otherwise they will work as singleton
  clearScope(AppComponent.testScope);
  
  //and use 'injectAsync' if object creation takes time or requires delays
  Logger fileLogger = await injectAsync();

  LogSender sender = inject();
  sender.sendLog();
}

Libraries

component
configuration
consts
errors
miodi
module
providers