ng_di 0.0.1+2

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 47

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

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

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or packages 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

Analysis

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

  • Dart: 2.0.0-dev.46.0
  • pana: 0.10.6

Scores

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

Platforms

Detected platforms: Flutter, web, other

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

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 2 errors 43 hints.

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

    line: 55 col: 10
    The expression in an assert must be of type 'bool'.

    Run dartfmt to format lib/ng_di.dart.

    Similar analysis of the following files failed:

    • lib/src/di/create_injector.dart (hint)
    • lib/src/di/injector/empty.dart (hint)
    • lib/src/di/injector/hierarchical.dart (hint)
    • lib/src/di/injector/map.dart (hint)
    • lib/src/di/injector/reflective.dart (hint)
    • lib/src/di/provider.dart (hint)
  • 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.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use ng_di.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0
build_runner ^0.4.0 0.4.0+3 0.8.2
built_mirrors ^0.5.0 0.5.2 0.8.4
logging ^0.11.3+1 0.11.3+1
meta ^1.1.1 1.1.2
Transitive dependencies
analyzer 0.30.0+4 0.31.1
args 1.4.1
async 1.13.3 2.0.6
barback 0.15.2+15
build 0.10.2+1 0.12.1
build_barback 0.4.0+1 0.5.0+3
charcode 1.1.1
cli_util 0.1.2+1
code_transformers 0.5.1+4
collection 1.14.9
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.11
glob 1.1.5
html 0.13.3
http_parser 3.1.1
isolate 1.1.0 2.0.0
kernel 0.3.0-alpha.1.1 0.3.0-alpha.11
mime 0.9.6
package_config 1.0.3
path 1.5.1
plugin 0.2.0+2
pool 1.3.4
shelf 0.6.8 0.7.2
shelf_static 0.2.7
source_gen 0.7.5+1 0.8.0
source_maps 0.10.4
source_span 1.4.0
stack_trace 1.9.2
stream_channel 1.6.4
stream_transform 0.0.11
string_scanner 1.0.2
typed_data 1.1.5
utf 0.9.0+4
watcher 0.9.7+7
yaml 2.1.13
Dev dependencies
test ^0.12.0