dartregistry 1.3.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 27

dartregistry

A simple dependency injection library inspired by Google Guice. Currently the library supports advanced futures as providers, provider functions, custom scope contexts, binding listeners, scope listeners.

Usage

A simple usage example:

import "package:dartregistry/dartregistry.dart";

@injectable
class ExampleModule extends RegistryModule {
  @override
  void configure() {
    // bind an instance
    bindInstance(Manager, new Manager());

    // bind a class implementation
    bindClass(Worker, Scope.ISOLATE, WorkerImpl);

    // bind a provider instance
    // bindProvider(Worker, Scope.ISOLATE, new WorkerProvider());

    // bind a provider function
    // bindProvideFunction(Worker, Scope.ISOLATE, () => new WorkerImpl());
  }
}

@injectable
class Manager {
  @inject
  Worker worker;

  // @inject
  @Inject(Worker)
  Provider<Worker> workerProvider;

  void test() {
    print("Manager test");

    // accessing directly
    worker.run();

    // accessing through a provider
    workerProvider.get().run();
  }
}

@injectable
abstract class Worker {
  void run();
}

@injectable
class WorkerImpl implements Worker {

  @onBind
  void init() {
    print("Worker bind");
  }

  @onUnbinding
  void deinit() {
    print("Worker unbind");
  }

  @override
  void run() {
    print("Worker run");
  }
}

@injectable
class WorkerProvider implements Provider<Worker> {
  @override
  Worker get() => new WorkerImpl();
}

main() async {
  // load the configuration
  Registry.load(new ExampleModule());

  // run inside the isolate scope context
  await Registry.runInIsolateScope(() {
    // lookup an object by its interface
    var manager = Registry.lookupObject(Manager) as Manager;
    manager.test();

    // lookups from in-scope bindings return the same instances
    var worker1 = Registry.lookupObject(Worker);
    var worker2 = Registry.lookupObject(Worker);
    assert(identical(worker1, worker2));
  });

  // unload the configuration
  Registry.unload();
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

1.13.1

  • upgrade reflectable to 1.0.0

example/example.dart

library example;

import "dart:async";

import "package:logging/logging.dart";

import "package:dartregistry/dartregistry.dart";

final Logger _libraryLogger = new Logger("example");

const Scope REQUEST_SCOPE = const Scope("REQUEST");

@injectable
class ExampleModule extends RegistryModule {
  @override
  void configure() {
    info("Configure module...");

    // provide a binding with a class
    bindProvider(Configuration, Scope.ISOLATE, new ConfigurationProvider());

    // bind an instance
    bindInstance(ConnectionManager, new ConnectionManager());

    // bind a class
    bindClass(ExampleService, Scope.ISOLATE, ExampleServiceImpl);

    // provide a binding with a function
    bindProvideFunction(Connection, REQUEST_SCOPE, () async {
      info("Provide a connection...");

      // simulate a delay
      await new Future.delayed(new Duration(seconds: 1));

      info("Connection provided");

      return new Connection();
    });

    info("Module configured");
  }

  @override
  void unconfigure() {
    info("Module unconfigured");
  }
}

main() async {
  Logger.root.level = Level.INFO;
  Logger.root.onRecord.listen((record) {
    print(record);
  });

  try {
    _libraryLogger.info("Begin example");

    Registry.load(new ExampleModule());

    try {
      await Registry.openScope(Scope.ISOLATE);

      await Registry.runInScope(REQUEST_SCOPE, () => request());
    } finally {
      await Registry.closeScope(Scope.ISOLATE);
    }
  } finally {
    Registry.unload();

    _libraryLogger.info("End example");
  }
}

Future request() async {
  _libraryLogger.info("Start request...");

  // static access on Registry for a lookup
  ExampleService service = Registry.lookupObject(ExampleService);

  var result = await service.execute("MY REQUEST");

  _libraryLogger.info("Request end with result: $result");
}

@injectable
class Configuration {
  final String data;

  Configuration(this.data);
}

@injectable
class ConfigurationProvider extends Loggable
    implements Provider<Future<Configuration>> {
  @override
  Future<Configuration> get() async {
    info("Provide a configuration...");

    // simulate a delay
    await new Future.delayed(new Duration(seconds: 1));

    info("Configuration provided");

    return new Configuration("CONFIGURATION");
  }

  @onBind
  void postBind() {
    info("ConfigurationProvider bound");
  }

  @onUnbinding
  void preUnbind() {
    info("Unbinding ConfigurationProvider");
  }

  @onProvidedBind
  void postProvidedBind(Future<Configuration> configurationFuture) {
    info("ConfigurationProvider postProvidedBind");

    configurationFuture.then((configuration) {
      info(
          "ConfigurationProvider postProvidedBind finish: ${configuration.data}");
    });
  }

  @onProvidedUnbinding
  void preProvidedUnbind(Future<Configuration> configurationFuture) {
    info("ConfigurationProvider preProvidedUnbind");

    configurationFuture.then((configuration) => info(
        "ConfigurationProvider preProvidedUnbind finish: ${configuration.data}"));
  }
}

@injectable
class ConnectionManager extends Loggable {
  // TODO
  @Inject(Configuration)
  Future<Configuration> configuration;

  @onScopeOpened
  Future configure() async {
    info("Configure connection manager...");

    // simulate a delay
    await new Future.delayed(new Duration(seconds: 1));

    info("Connection manager configured");
  }

  Connection createConnection() {
    info("Create connection");

    return new Connection();
  }

  @onScopeClosing
  Future close() async {
    info("Closing connection manager...");

    // simulate a delay
    await new Future.delayed(new Duration(seconds: 1));

    info("Connection manager closed");
  }
}

@injectable
class Connection extends Loggable {
  Future<String> query(String query) async {
    info("Start query");

    // simulate a delay
    await new Future.delayed(new Duration(seconds: 1));

    info("End query");

    return "RESULT OF: $query";
  }

  @onScopeClosing
  Future close() async {
    info("Closing connection...");

    // simulate a delay
    await new Future.delayed(new Duration(seconds: 1));

    info("Connection closed");
  }
}

@injectable
abstract class ExampleService {
  Future<String> execute(String request);
}

@injectable
class ExampleServiceImpl extends Loggable implements ExampleService {

  @Inject(Connection)
  Provider<Connection> connectionProvider;

  Future<String> execute(String request) async {
    info("Executing request...");

    // simulate a delay
    await new Future.delayed(new Duration(seconds: 1));

    info("Executed!");

    return request;
  }
}

1. Depend on it

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


dependencies:
  dartregistry: "^1.3.1"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:dartregistry/dartregistry.dart';
        
Version Uploaded Documentation Archive
1.3.1 Sep 12, 2016 Go to the documentation of dartregistry 1.3.1 Download dartregistry 1.3.1 archive
1.3.0 Feb 15, 2016 Go to the documentation of dartregistry 1.3.0 Download dartregistry 1.3.0 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Feb 24, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
0 / 100
Health:
Code health derived from static analysis. [more]
55 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
52 / 100
Overall score:
Weighted score of the above. [more]
27

Platforms

Detected platforms: web, other

Primary library: package:dartregistry/dartregistry.dart with components: mirrors.

Suggestions

  • Fix lib/src/registry.dart.

    Strong-mode analysis of lib/src/registry.dart failed with the following error:

    line: 604 col: 56
    The getter 'instance' isn't defined for the class 'Provider'.

  • Fix issues reported by dartanalyzer.

    dartanalyzer reported 1 error(s) and 0 warning(s).

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.12.0 <2.0.0
logging ^0.11.0 0.11.3+1
reflectable ^1.0.0 1.0.4 2.0.0-dev.1.0
Transitive dependencies
analyzer 0.30.0+4 0.31.1
args 1.4.1
async 2.0.3 2.0.4
barback 0.15.2+14
charcode 1.1.1
cli_util 0.1.2+1
code_transformers 0.5.1+4
collection 1.14.5
convert 2.0.1
crypto 2.0.2+1
csslib 0.14.1
dart_style 1.0.9+1 1.0.10
front_end 0.1.0-alpha.4.1 0.1.0-alpha.10
glob 1.1.5
html 0.13.2+2
isolate 1.1.0
kernel 0.3.0-alpha.1.1 0.3.0-alpha.10
meta 1.1.2
package_config 1.0.3
path 1.4.2 1.5.1
plugin 0.2.0+2
pool 1.3.4
source_maps 0.10.4
source_span 1.4.0
stack_trace 1.9.2
string_scanner 1.0.2
typed_data 1.1.5
utf 0.9.0+4
watcher 0.9.7+7
yaml 2.1.13