dependency injector for flutter that works out of the box without mirrors. Used https://github.com/dotdotcommadot/dart_robotlegs_di as a base to do this simple no mirrors implementation.
Use Injector.instance
singleton to access the injector, or wrap it inside your own singleton.
map type to a already instantiated value.
injector.mapToValue(CounterModel, new CounterModel());
Optionally add name.
injector.mapToValue(ChangeNotifier,new ChangeNotifier(), name: 'counter_changed');
One advice can be to wrap basic types to distinct classes to avoid confusion.
Injector will return instance that was previously mapped
CounterModel counterModel = injector.getInstance(CounterModel) as Counter;
ChangeNotifier changeNotifier = injector.getInstance(ChangeNotifier,'counter_changed') as ChangeNotifier;
map to a provider
class SecureRandomProvider implements IProvider{
final Random secure = Random.secure();
@override
void destroy() {
}
@override
apply({Type type}) => SecureRandom(secure.nextDouble());
}
class SecureRandom {
final double random;
const SecureRandom(this.random);
}
injector.mapToProvider(SecureRandom,new SecureRandomProvider());
later when you inject into SecureRandom, you will always get unique crypto secure double.
double SecureRandom secureRandom = injector.getInstance(SecureRandom) as SecureRandom;
If you want to provide dependecies of previously mapped values to your mapping, implement IInjectable into the class. Injector will then auto populate mappings
Implement IInjectable
to provide chain injection.
class CounterModel implements IInjectable {
@override
void inject(IInjector injector){
//get your mappings here
}
}
get it on pub pub.
and check example project github.
great thanks to robotlegs 2 team for actionscript and https://github.com/dotdotcommadot/dart_robotlegs_di for starting this implementation in dart.
Add this to your package's pubspec.yaml file:
dependencies:
organic_injector: ^0.0.4
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.
Now in your Dart code, you can use:
import 'package:organic_injector/organic_injector.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.0.4 | Mar 28, 2018 |
|
|
0.0.3 | Mar 17, 2018 |
|
|
0.0.2 | Mar 17, 2018 |
|
|
0.0.1 | Mar 17, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
22
|
Health:
Code health derived from static analysis.
[more]
|
--
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
--
|
Overall:
Weighted score of the above.
[more]
|
11
|
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.
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.
Make sure dartdoc
successfully runs on your package's source files. (-10 points)
Dependencies were not resolved.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.20.1 <2.0.0 |