kiwi_generator 0.2.0

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

kiwi_generator

Pub Donate

Logo

Generates dependency injection code using the kiwi package to reduce development time.

Configuration

  1. Add kiwi to pubspec.yaml under the dependencies: section. The latest version is Pub
dependencies:
  kiwi: ^latest_version
  1. Add build_runner and kiwi_generator under the dev_dependencies: section of the pubspec.yaml file. The latest version is Pub
dev_dependencies:  
  build_runner: '>=0.10.3 <1.2.0'
  kiwi_generator: ^latest_version
  1. Add (or modify) the build.yaml file in the same folder as the pubspec.yaml and include the kiwi builder.
targets:
  $default:
    builders:
      kiwi:

Usage

In your library add the following import:

import 'package:kiwi/kiwi.dart';

Create an abstract class with an abstract method:

abstract class Injector {
  void configure();
}

Annotate the abstract method with the kiwi Register annotations.

abstract class Injector {  
  @Register.singleton(ServiceA)
  @Register.factory(Service, from: ServiceB)
  @Register.factory(ServiceB, name: 'factoryB')
  @Register.factory(ServiceC, resolvers: {ServiceB: 'factoryB'})
  void configure();
}

Include the part directive indicating the file that will be generated (typically the same file with a .g extension before .dart):

part 'test01.g.dart';

Run build_runner:

pub run build_runner build

For Flutter the command is different though:

flutter packages pub run build_runner build

Note: On first attempt to run this command you might encounter a conflict error. If so, please add the --delete-conflicting-outputs argument to your command:

flutter packages pub run build_runner build --delete-conflicting-outputs

(This additional argument allows the command to overwrite the .g.dart file if necessary.)

You can also use the watch command instead of build. This will generate your file when it's saved.

pub run build_runner watch

A concrete class named _$TheNameOfYourAbstractClass will be generated and you can call the method where you like. For example you can create a function in your library which will call it:

void setup() {
  var injector = _$Injector();
  injector.configure();
}

Or you can create a function that will return the concrecte injector and use it elsewhere:

Injector getInjector() => _$Injector();

Annotations

There is only one annotation, called Register, with two constructors: factory and singleton. There are no constructor for registering instances because only const instances are supported in metadata. And it would'nt be easier to create an annotation than registering directly with a container.

If you want to register a singleton (the factory will be called only one time, when accessing it for the first time):

@Register.singleton(ServiceA)

If you want to register a factory:

@Register.factory(ServiceA)

Both constructors have the same parameters:

ParameterTypeRequiredDescription
typeTypeYesThis is the type to register
nameStringNoThis is the name under which the factory will be registered
fromTypeNoThe type to create when requesting type, if different of type.
constructorNameStringNoThe name of the constructor to use inside the factory
resolversMap<String, String>NoA map that give for a type, the name under which it should be resolved

Short example

This code:

import 'package:kiwi/kiwi.dart';

part 'test01.g.dart';

abstract class Injector {
  @Register.singleton(ServiceA)
  @Register.factory(Service, from: ServiceB)
  @Register.factory(ServiceB, name: 'factoryB')
  @Register.factory(ServiceC, resolvers: {ServiceB: 'factoryB'})
  void common();

  @Register.factory(ServiceC)
  void development();

  @Register.factory(ServiceC, constructorName: 'other')
  void production();
}

class Service {}

class ServiceA extends Service {}

class ServiceB extends Service {
  ServiceB(ServiceA serviceA);
}

class ServiceC extends Service {
  ServiceC(ServiceA serviceA, ServiceB serviceB);
  ServiceC.other(ServiceB serviceA);
}

void setup(bool isProduction) {
  var injector = _$Injector();
  injector.common();
  if (isProduction) {
    injector.production();
  } else {
    injector.development();
  }
}

Will produce this:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'test01.dart';

// **************************************************************************
// InjectorGenerator
// **************************************************************************

class _$Injector extends Injector {
  void common() {
    final Container container = Container();
    container.registerSingleton((c) => ServiceA());
    container
        .registerFactory<Service, ServiceB>((c) => ServiceB(c<ServiceA>()));
    container.registerFactory((c) => ServiceB(c<ServiceA>()), name: 'factoryB');
    container.registerFactory(
        (c) => ServiceC(c<ServiceA>(), c<ServiceB>('factoryB')));
  }

  void development() {
    final Container container = Container();
    container.registerFactory((c) => ServiceC(c<ServiceA>(), c<ServiceB>()));
  }

  void production() {
    final Container container = Container();
    container.registerFactory((c) => ServiceC.other(c<ServiceB>()));
  }
}

Changelog

Please see the Changelog page to know what's recently changed.

0.2.0

Fixed

  • Upgrade dependencies

0.1.1

Fixed

  • The generator no longer generates a .g.dart file for abstract classes without abstract methods.

0.1.0

  • Initial Open Source release.

example/kiwi_generator_example.dart

import 'package:kiwi/kiwi.dart';

part 'kiwi_generator_example.g.dart';

abstract class Injector {
  @Register.singleton(ServiceA)
  @Register.factory(Service, from: ServiceB)
  @Register.factory(ServiceB, name: 'factoryB')
  @Register.factory(ServiceC, resolvers: {ServiceB: 'factoryB'})
  void configure();
}

class Service {}

class ServiceA extends Service {}

class ServiceB extends Service {
  ServiceB(ServiceA serviceA);
}

class ServiceC extends Service {
  ServiceC(ServiceA serviceA, ServiceB serviceB);
  ServiceC.other(ServiceB serviceA);
}

void setup() {
  var injector = _$Injector();
  injector.configure();
}

Use this package as a library

1. Depend on it

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


dependencies:
  kiwi_generator: ^0.2.0

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:kiwi_generator/kiwi_generator.dart';
  
Version Uploaded Documentation Archive
0.2.0 Nov 3, 2018 Go to the documentation of kiwi_generator 0.2.0 Download kiwi_generator 0.2.0 archive
0.1.1 Sep 11, 2018 Go to the documentation of kiwi_generator 0.1.1 Download kiwi_generator 0.1.1 archive
0.1.0 Sep 9, 2018 Go to the documentation of kiwi_generator 0.1.0 Download kiwi_generator 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
51
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]
75
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.12.5

Platforms

Detected platforms: other

Primary library: package:kiwi_generator/kiwi_generator.dart with components: io, build, mirrors.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.32.4 <0.34.0 0.33.2
build >=0.12.6 <1.1.0 1.0.1
build_config ^0.3.1 0.3.1+4
code_builder ^3.1.3 3.1.3
dart_style >=1.0.0 <1.3.0 1.2.0
kiwi >=0.1.0 <0.2.0 0.1.0
path ^1.6.2 1.6.2
source_gen ^0.9.1 0.9.1+3
Transitive dependencies
args 1.5.0
async 2.0.8
built_collection 4.0.0
built_value 6.1.5
charcode 1.1.2
collection 1.14.11
convert 2.0.2
crypto 2.0.6
csslib 0.14.6
fixnum 0.10.8
front_end 0.1.6+2
glob 1.1.7
html 0.13.3+3
json_annotation 2.0.0
kernel 0.3.6+2
logging 0.11.3+2
matcher 0.12.4
meta 1.1.6
package_config 1.0.5
plugin 0.2.0+3
pub_semver 1.4.2
pubspec_parse 0.1.2+3
quiver 2.0.1
source_span 1.4.1
stack_trace 1.9.3
string_scanner 1.0.4
typed_data 1.1.6
utf 0.9.0+5
watcher 0.9.7+10
yaml 2.1.15
Dev dependencies
build_runner ^1.1.0
build_test ^0.10.3
test ^1.0.0