dependencies 0.1.5

Mentioned in Awesome Dart CircleCI pub package

This is a simple and flexible dependency injection container for dart.

If you are using Flutter consider the dependencies_flutter package.

Features #

  • Immutability: The injector is created via a builder so it won't be modified after being created.
  • Modules: Separate dependencies into logical pars. Useful to separate by environments.
  • Singletons: Bind singletons and lazy singletons.
  • Factories: Bind factories.
  • Named dependencies: Give your dependencies custom names.
  • Extra arguments: Pass arguments to your factory methods.
  • Works with Flutter: Since it doesn't use reflexion it works with Flutter.

Usage #

Set up #

Optionally create a module.

class PlayerModule extends Module {
  @override
  void configure(Binder binder) {
    binder
      ..bindSingleton("playerkey", name: "api_key")
      ..bindFactory((injector, params) => Player(params["playerId"]))
      ..bindLazySingleton((injector, params) => Rest(injector));
  }
}

Create a builder and use it to instantiate an injector.

final builder = Injector.builder()
    ..bind(instance: "abc123", isSingleton: true, name: "api_key")
    ..bindSingleton("abc123", name: "api_key_2")
    ..bindFactory((injector, params) => User(params["userId"]))
    ..bindLazySingleton((injector, params) => RestController(params["path"]))
    ..install(PlayerModule());

final injector = builder.build();

Usage #

Get instances from the container.

final Player player = injector.get(params: {"playerId":1});
final Rest rest = injector.get();
final String apiKey = injector.get(name: "api_key");

Optionally you can manage injectors with the registry. Feel free to create your own or use the singleton.

InjectorRegistry.instance.register(injector);

Injectors can be fetched from the registry.

final injector = InjectorRegistry.instance.get();
final namedInjector = InjectorRegistry.instance.get(name: "named_injector");

0.0.1 #

  • Initial version.

0.0.2 #

  • Added testing.
  • Fixed a bug with registrations.

0.0.3 #

  • Added better binding interfaces for factories and singletons.
  • Added examples.

0.0.4 #

  • Added shortcuts to inject through the InjectorRegistry.
  • Added Flutter version.

0.0.5 #

  • Added configurer and installer.
  • Removed global allow override.

0.1.0 #

  • Added disposing mechanism.
  • Stable API

0.1.1 #

  • Added the possibility to create an injector from bindings.

example/example.dart

import 'package:dependencies/dependencies.dart';

void main() {
  final builder = Injector.builder()
    ..bindSingleton("abc123", name: "api_key")
    ..bindLazySingleton((i, p) => Rest(i.get(name: "api_key")))
    ..install(PlayerModule());

  InjectorRegistry.instance.register(builder.build());
  final injector = InjectorRegistry.instance.get();

  final player1 = injector.get<Player>(params: Params.fromMap({"id": "1"}));
  print(player1);
  final player2 = injector.get<Player>(params: Params.fromMap({"id": "2"}));
  print(player2);
  final key = injector.get<String>(name: "api_key");
  print(key);
  final rest = injector.get<Rest>();
  print(rest);
}

class PlayerModule implements Module {
  @override
  void configure(Binder binder) {
    binder..bindFactory((i, p) => Player(p.get("id")));
  }
}

class Player {
  final String id;

  Player(this.id);

  @override
  String toString() {
    return 'Player{id: $id}';
  }
}

class Rest {
  final String key;

  Rest(this.key);

  @override
  String toString() {
    return 'Rest{key: $key}';
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  dependencies: ^0.1.5

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:dependencies/dependencies.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
84
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
92
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
disposables ^0.1.2 0.1.2
quiver ^2.0.1 2.0.3
Transitive dependencies
matcher 0.12.5
meta 1.1.7
path 1.6.2
stack_trace 1.9.3
Dev dependencies
test ^1.0.0

Admin