ioc 0.1.0 ioc: ^0.1.0 copied to clipboard
A starting point for Dart libraries or applications.
Ioc #
The most simple Ioc Container of Dart and Flutter
Goal #
Keep it minimal
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
- 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
}