ng_di 0.0.1+2

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 7

ng_di

Build Status

A Dependency Injection package taken from angular DI.

Usage

  1. create a file tool/build.dart and add next code:
import 'package:build_runner/build_runner.dart';
import 'package:built_mirrors/phase.dart';


main() async {
  // In next line replace `example/**.dart` for the globs you want to use as input, for example `**/*.dart`
  // or leave it empty to take all the dart files of the project as input.
  await build([builtMirrorsAction(const ['example/**.dart'])], deleteFilesByDefault: true);
}

  1. create one of the examples shown below

  2. run tool/build.dart

Inject by Type

In this case you need to annotate classes with @Injectable() or @injectable. This annotation tells to library built-mirrors to generate respective mirrors. For example:

library example.type;

import 'package:built_mirrors/built_mirrors.dart';
import 'package:ng_di/ng_di.dart';

part 'type.g.dart';

@injectable
class Engine {
  String name;

  Engine();
}

@Injectable()
class Car {
  final Engine engine;

  Car(this.engine);
}

main() {
  _initMirrors();

  var injector = createInjector([Engine, Car]);

  assert(injector.get(Car).engine.name == null);
  print('Carg.engine: ${injector.get(Car).engine.name}');
}

Inject using @Inject() annotation

In this case you can specify the token corresponding to the dependency that will be injected. For example:

library example.inject;

import 'package:built_mirrors/built_mirrors.dart';
import 'package:ng_di/ng_di.dart';

part 'inject.g.dart';

class Engine {
  String name;

  Engine([this.name]);
}

@Injectable()
class Car {
  final Engine engine;

  Car(@Inject("MyEngine") this.engine);
}

main() {
  _initMirrors();

  var engine = new Engine('my-engine');

  var injector = createInjector([
    new Provider("MyEngine", useValue: engine),
    Car
  ]);

  assert(injector.get(Car).engine.name == engine.name);
  print('Carg.engine: ${injector.get(Car).engine.name}');
}

Inject using @Optional() or @optional annotation

In this case we can mark dependencies as optional, so if the value is not injected then a null value is passed. For example:

library example.optional;

import 'package:built_mirrors/built_mirrors.dart';
import 'package:ng_di/ng_di.dart';

part 'optional.g.dart';

@injectable
class Engine {
  String name;

  Engine(@Optional() this.name);
}

@Injectable()
class Car {
  final Engine engine;

  Car(this.engine);
}

main() {
  _initMirrors();

  var injector = createInjector([
    Engine,
    Car
  ]);

  assert(injector.get(Car).engine.name == null);
  print('Carg.engine: ${injector.get(Car).engine.name}');
}

Inject using @Self() or @self annotation

In this case we can mark dependencies as self, so the injector should retrieve a dependency only from itself. For example:

library example.self;

import 'package:built_mirrors/built_mirrors.dart';
import 'package:ng_di/ng_di.dart';

part 'self.g.dart';

@injectable
class Engine {
  String name;

  Engine();
}

@Injectable()
class Car {
  final Engine engine;

  Car(@Self() this.engine);
}

main() {
  _initMirrors();

  var injector = createInjector([Engine, Car]);

  assert(injector.get(Car).engine.name == null);
  print('Car.engine: ${injector.get(Car).engine.name}');

  resetCache();
  injector = createInjector([Engine]);
  var child = createInjector([Car], injector);

  try {
    print('Car.engine: ${child.get(Car).engine.name == null}');
  } catch (e) {
    print('error: $e');
  }
}

Inject using @SkipSelf() or @skipSelf annotation

In this case we can mark dependencies as optional, so the dependency resolution should start from the parent injector. For example:

library example.skip_self;

import 'package:built_mirrors/built_mirrors.dart';
import 'package:ng_di/ng_di.dart';

part 'skip_self.g.dart';

@injectable
class Engine {
  String name;

  Engine();
}

@Injectable()
class Car {
  final Engine engine;

  Car(@SkipSelf() this.engine);
}

main() {
  _initMirrors();

  var injector = createInjector([Engine, Car]);

  try {
    print('Car.engine: ${injector.get(Car).engine.name == null}');
  } catch (e) {
    print('error: $e');
  }

  resetCache();

  injector = createInjector([Engine]);
  var child = createInjector([Car], injector);

  assert(child.get(Car).engine.name == null);
  print('Car.engine: ${child.get(Car).engine.name}');
}

0.0.1

  • first commit. Taken from angular@4.0.0
  • add createInjector function
  • use built_mirrors

Use this package as a library

1. Depend on it

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


dependencies:
  ng_di: ^0.0.1+2

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:ng_di/ng_di.dart';
  
Version Uploaded Documentation Archive
0.0.1+2 Aug 31, 2017 Go to the documentation of ng_di 0.0.1+2 Download ng_di 0.0.1+2 archive
0.0.1+1 Aug 31, 2017 Go to the documentation of ng_di 0.0.1+1 Download ng_di 0.0.1+1 archive
0.0.1 Aug 31, 2017 Go to the documentation of ng_di 0.0.1 Download ng_di 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
13
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
7
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0