From 10b6011abaa4235d7587177d46df5b32fbc56cec Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 7 Nov 2018 21:50:04 -0500 Subject: [PATCH] Add mountController + test --- CHANGELOG.md | 1 + lib/src/core/controller.dart | 4 +++- lib/src/core/server.dart | 10 ++++++++++ test/controller_test.dart | 8 +++++++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98e5ca69..3fbe677b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 2.0.0-alpha.11 * Add `readMany` to `Service`. * Allow `ResponseContext.redirect` to take a `Uri`. +* Add `Angel.mountController`. # 2.0.0-alpha.10 * All calls to `Service.parseId` are now affixed with the `` argument. diff --git a/lib/src/core/controller.dart b/lib/src/core/controller.dart index 4d6f71a4..65cf262f 100644 --- a/lib/src/core/controller.dart +++ b/lib/src/core/controller.dart @@ -30,7 +30,9 @@ class Controller { _app = app; if (injectSingleton != false) { - _app.container.registerSingleton(this, as: runtimeType); + if (!app.container.has(runtimeType)) { + _app.container.registerSingleton(this, as: runtimeType); + } } // Load global expose decl diff --git a/lib/src/core/server.dart b/lib/src/core/server.dart index 503bcf42..39ab2024 100644 --- a/lib/src/core/server.dart +++ b/lib/src/core/server.dart @@ -333,6 +333,16 @@ class Angel extends Routable { return new Future.sync(() => configurer(this)); } + /// Shorthand for using the [container] to instantiate, and then mount a [Controller]. + /// + /// Just like [Container].make, in contexts without properly-reified generics (dev releases of Dart 2), + /// provide a [type] argument as well. + /// + /// If you are on `Dart >=2.0.0`, simply call `mountController()`.. + Future mountController([Type type]) { + return configure(container.make(type).configureServer); + } + /// Shorthand for calling `all('*', handler)`. Route fallback(RequestHandler handler) { return all('*', handler); diff --git a/test/controller_test.dart b/test/controller_test.dart index cc0f9f24..1f1a42ba 100644 --- a/test/controller_test.dart +++ b/test/controller_test.dart @@ -59,7 +59,13 @@ main() { "/redirect", (req, res) async => res.redirectToAction("TodoController@foo", {"foo": "world"})); - await app.configure((ctrl = new TodoController()).configureServer); + + // Register as a singleton, just for the purpose of this test + if (!app.container.has()) + app.container.registerSingleton(ctrl = new TodoController()); + + // Using mountController(); + await app.mountController(); print(app.controllers); app.dumpTree();