ioc 0.3.0 ioc: ^0.3.0 copied to clipboard
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 packa [...]
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 main 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
}
using singleton:
import 'package:ioc/ioc.dart';
class A {
void someMethod() {}
}
main() {
// use singleton on one
Ioc().bind('A', (ioc) => new A(), singleton: true);
Ioc().use<A>('A').someMethod();
// use singleton on all
Ioc().config['singlton'] = true;
Ioc().use<A>('A').someMethod();
}
using lazy-loading singleton:
import 'package:ioc/ioc.dart';
class A {
void someMethod() {}
}
main() {
// use lazy loaded singleton on one
Ioc().bind('A', (ioc) => new A(), singleton: true, lazy: true);
// class A will only be instantiated after first .use('A')
Ioc().use<A>('A').someMethod();
// use lazy loaded singleton on all
Ioc().config['lazy'] = true;
Ioc().use<A>('A').someMethod();
}