kiwi_generator

Creator: coderz1093

Last updated:

Add to Cart

Description:

kiwi generator

kiwi_generator #


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

Add kiwi to pubspec.yaml under the dependencies: section.
The latest version is

dependencies:
kiwi: ^latest_version
copied to clipboard

Add build_runner and kiwi_generator under the dev_dependencies: section of the pubspec.yaml file.
The latest version is

dev_dependencies:
build_runner: ^2.3.3
kiwi_generator: ^latest_version
copied to clipboard
Usage #
In your library add the following import:
import 'package:kiwi/kiwi.dart';
copied to clipboard
Create an abstract class with an abstract method:
abstract class Injector {
void configure();
}
copied to clipboard
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();
}
copied to clipboard
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';
copied to clipboard
Run build_runner:
pub run build_runner build
copied to clipboard
For Flutter the command is different though:
flutter packages pub run build_runner build
copied to clipboard
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
copied to clipboard
(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
copied to clipboard
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();
}
copied to clipboard
Or you can create a function that will return the concrecte injector and use it elsewhere:
Injector getInjector() => _$Injector();
copied to clipboard
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)
copied to clipboard
If you want to register a factory:
@Register.factory(ServiceA)
copied to clipboard
Both constructors have the same parameters:



Parameter
Type
Required
Description




type
Type
Yes
This is the type to register


name
String
No
This is the name under which the factory will be registered


from
Type
No
The type to create when requesting type, if different of type.


constructorName
String
No
The name of the constructor to use inside the factory


resolvers
Map<String, String>
No
A 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();
}
}
copied to clipboard
Will produce this:
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'test01.dart';

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

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

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

void production() {
final KiwiContainer container = KiwiContainer();
container.registerFactory((c) => ServiceC.other(c<ServiceB>()));
}
}
copied to clipboard
Changelog #
Please see the Changelog page to know what's recently changed.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.