simple_injector 0.1.0 simple_injector: ^0.1.0 copied to clipboard
Simple Injector DI
SimpleInjector #
Simple Injector DI
Usage #
To use this plugin, add simple_injector
as a dependency in your pubspec.yaml file.
Example #
Before configure is necessary create module of the inject. Example:
import 'package:simple_injector/module_injector.dart';
import 'package:simple_injector/simple_injector.dart';
class RepositoryModulo extends ModuleInjector{
///in the constructor we add what should be injected by passing: type and the method that creates it (optional isSingleton if desired)
RepositoryModulo(){
add(RepositoryTest, repositoryTest, isSingleton: true);
add(RepositoryTest2, repositoryTest2);
}
RepositoryTest repositoryTest(){
return RepositoryTestImpl();
}
RepositoryTest2 repositoryTest2(){
return RepositoryTest2Impl(
SimpleInjector().inject()
);
}
}
After the module created we configured our SimpleInjector in the constructor app
import 'package:simple_injector/simple_injector.dart';
MyApp(){
SimpleInjector.configure(Flavor.PROD);
SimpleInjector().registerModule(RepositoryModulo());
}
All ready! you can register as many modules as you want. to inject air into a configured dependency, just add this:
RepositoryTest repository = SimpleInjector().inject();
or
final repository = SimpleInjector().<RepositoryTest>inject();