Skip to content
Simple and flexible dependency injection container for dart.
Dart
Branch: master
Clone or download
marcguilera Merge pull request #3 from georgeci/master
Fix README.md. Replace "extends" with "implements"
Latest commit d77e227 Nov 6, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Fix circle ci Oct 29, 2018
.github/workflows Add pipeline Sep 27, 2019
example 0.1.0 Nov 4, 2018
lib fix NPE Feb 20, 2019
test/src/builder injector constructors Nov 4, 2018
.gitignore 0.1.0 Nov 4, 2018
CHANGELOG.md Added injector constructor Nov 4, 2018
LICENSE.md license Oct 28, 2018
README.md Fix README.md. Replace "extends" with "implements" Jul 20, 2019
analysis_options.yaml initial version Oct 28, 2018
pubspec.yaml fix NPE Feb 20, 2019

README.md

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 implements 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");
You can’t perform that action at this time.