ioc 0.2.1 ioc: ^0.2.1 copied to clipboard
The most simple Ioc Container of Dart and Flutter
IoC #
The most simple IoC Container of Dart and Flutter
If you looking for a package that is light-weight and provide production-ready of inversion of control, then this package is right for you.
This package provides only 2 api, easy to learn and use, but definitely fits most use case in your flutter project.
If you are a server developer developing small or medium scale project, it's very likely you want to use this package. However, large scale project may need more powerful heavy-weight IoC library.
You can use it in your angular project, but we highly recommend you use dependency injection system provided by angular.
Goal #
Keep it minimal, light-weight
Important #
- The container does not rely on reflection, it's just a Map, so supports Flutter
- Dart2 is required to use this package
Feature #
- Supports singleton
- Supports binding to anything
- Supports lazy initialization (Developing)
- Detects circular dependencies (Developing)
Usage #
bind to a string:
import 'package:ioc/ioc.dart';
main() {
Ioc().bind('MyClass', (ioc) => new MyClass());
// later
Ioc().use('MyClass'); // you will get an instance of MyClass
// with generic if you want
Ioc().use<MyClass>('MyClass');
}
bind to self:
import 'package:ioc/ioc.dart';
main() {
Ioc().bind(MyClass, (ioc) => new MyClass());
// later
Ioc().use(MyClass); // you will get an instance of MyClass
}
bind to other:
import 'package:ioc/ioc.dart';
main() {
Ioc().bind(MyClass, (ioc) => new OtherClass());
// later
Ioc().use(MyClass); // you will get an instance of OtherClass
}
bind with other dependency
import 'package:ioc/ioc.dart';
main() {
Ioc().bind('MyClass', (Ioc ioc) {
OtherClass other = ioc.use<OtherClass>('OtherClass');
return new MyClass(other);
});
// later
Ioc().use<MyClass>('MyClass'); // you will get an instance of OtherClass
}