Redstone.dart uses the di package to provide dependency injection.
To register a module, use the addModule()
method:
import 'package:redstone/redstone.dart' as app;
import 'package:di/di.dart';
main() {
app.addModule(new Module()
..bind(ClassA)
..bind(ClassB));
app.setupConsoleLog();
app.start();
}
For methods annotated with @Route
, you can inject objects using the @Inject
annotation:
@app.Route('/service')
service(@app.Inject() ClassA objA) {
...
}
Groups can require objects using a constructor:
@app.Group('/group')
class Group {
ClassA objA;
Group(ClassA this.objA);
@app.Route('/service')
service() {
// ...
}
}
Interceptors and error handlers can also require dependencies:
@app.Interceptor(r'/services/.+')
interceptor(ClassA objA, ClassB objB) {
// ...
}
@app.ErrorHandler(404)
notFound(ClassB objB) {
// ...
}