Dado is a dependency injection framework for Dart.
Dado attempts to have minimal set of features and a syntax that takes advantage of Dart, which makes it different from many other popular DI frameworks.
Dado tries to make DI more lightweight by letting you define modules as Dart classes and as declaratively as possible. Bindings can be define by simply declaring an abstract method:
class MyModule extends Module {
Foo get foo;
}
Then you can create an injector and get an instance of Foo:
var injector = new Injector([MyModule]);
injector.getInstanceOf(Foo);
Or call an injected closure:
injector.callInjected((Foo foo) {
print("foo is $foo");
});
See the tests for more examples.
See the dartdoc documentation for Dado
Use Pub and simply add the following to your pubspec.yaml
file:
dependencies:
dado: 0.5.1
You can find more details on the Dado page on Pub
import 'package:dado/dado.dart';
class MyModule extends Module {
// binding to an instance, similar to bind().toInstance() in Guice
String serverAddress = "127.0.0.1";
// Getters define a singleton, similar to bind().to().in(Singleton.class)
// in Guice
Foo get foo;
// Methods define a factory binding, similar to bind().to() in Guice
Bar newBar();
// Methods that delegate to bindTo() bind a type to a specific
// implementation of that type
Baz get baz => bindTo(Baz).singleton;
// Bindings can be made to provider methods
Qux newQux() => bindTo(Qux)
.providedBy((Foo foo) => new Qux(foo, 'not injected')).newInstance();
}
class Bar {
// When there is only one constructor, it is automatically injected with
// dependencies
Bar(Foo foo);
}
class Baz {
String serverAddress;
Baz();
// In classes that have multiple constructors, the desired constructor can
// be selected using the @inject annotation. Otherwise, Dado will look for
// a no-args constructor.
@inject
Baz.injectable(String this.serverAddress);
}
main() {
var injector = new Injector([MyModule]);
Bar bar = injector.getInstance(Bar);
}
Dado is under active development. It has a few tests, but has not been used in production yet.
Module
. When mixins are better supported in Dart,Module
can be mixed in instead.