dependencies 0.1.4

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

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.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
0.1.4 Nov 14, 2018 Go to the documentation of dependencies 0.1.4 Download dependencies 0.1.4 archive
0.1.3 Nov 13, 2018 Go to the documentation of dependencies 0.1.3 Download dependencies 0.1.3 archive
0.1.2 Nov 4, 2018 Go to the documentation of dependencies 0.1.2 Download dependencies 0.1.2 archive
0.1.1 Nov 4, 2018 Go to the documentation of dependencies 0.1.1 Download dependencies 0.1.1 archive
0.1.0 Nov 4, 2018 Go to the documentation of dependencies 0.1.0 Download dependencies 0.1.0 archive
0.0.5 Oct 31, 2018 Go to the documentation of dependencies 0.0.5 Download dependencies 0.0.5 archive
0.0.4 Oct 30, 2018 Go to the documentation of dependencies 0.0.4 Download dependencies 0.0.4 archive
0.0.3 Oct 30, 2018 Go to the documentation of dependencies 0.0.3 Download dependencies 0.0.3 archive
0.0.2 Oct 30, 2018 Go to the documentation of dependencies 0.0.2 Download dependencies 0.0.2 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
62
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]
81
Learn more about scoring.

We analyzed this package on Nov 14, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6

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.1
Transitive dependencies
matcher 0.12.4
meta 1.1.6
path 1.6.2
stack_trace 1.9.3
Dev dependencies
test ^1.0.0