organic_injector 0.0.1 organic_injector: ^0.0.1 copied to clipboard
Simple dependecy injector for dart and flutter. Inspired by SwiftSuspenders and robotlegs_di.
organic_injection #
dependency injector for flutter that works out of the box
Getting Started #
map type to a already instantiated value.
injector.mapToValue(CounterModel, new CounterModel());
Optionally add name.
injector.mapToValue(ChangeNotifier,new ChangeNotifier(), name: 'counter_changed');
Note that we never add
Injector will return instance that was previously mapped
Counter model = 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
}
}
check https://github.com/matejthetree/color_meditation for example usage place them both inside the same folder, as this repo is not yet published to pub
TODO:
- implement build script so that you can inject only using annotation such as
@inject
,@preConstruct
,@postConstruct
@destroy
, - add tests
great thanks to robotlegs 2 team for actionscript and @dotdotcomadot for starting this implementation in dart.