diff --git a/angel_container/CHANGELOG.md b/angel_container/CHANGELOG.md index 9151fcb4..63a7658b 100644 --- a/angel_container/CHANGELOG.md +++ b/angel_container/CHANGELOG.md @@ -3,6 +3,8 @@ * Add `ThrowingReflector`, which throws on all operations. * `EmptyReflector` uses `Object` instead of `dynamic` as its returned type, as the `dynamic` type is (apparently?) no longer a valid constant value. +* `registerSingleton` now returns the provided `object`. +* `registerFactory` and `registerLazySingleton` now return the provided function `f`. # 1.0.4 * Slight patch to prevent annoying segfault. diff --git a/angel_container/lib/src/container.dart b/angel_container/lib/src/container.dart index a80b7f48..c28c8187 100644 --- a/angel_container/lib/src/container.dart +++ b/angel_container/lib/src/container.dart @@ -141,8 +141,11 @@ class Container { /// Shorthand for registering a factory that injects a singleton when it runs. /// /// In many cases, you might prefer this to [registerFactory]. - void registerLazySingleton(T Function(Container) f, {Type as}) { - registerFactory( + /// + /// Returns [f]. + T Function(Container) registerLazySingleton(T Function(Container) f, + {Type as}) { + return registerFactory( (container) { var r = f(container); container.registerSingleton(r, as: as); @@ -152,7 +155,11 @@ class Container { ); } - void registerFactory(T Function(Container) f, {Type as}) { + /// Registers a factory. Any attempt to resolve the + /// type within *this* container will return the result of [f]. + /// + /// Returns [f]. + T Function(Container) registerFactory(T Function(Container) f, {Type as}) { as ??= T; if (_factories.containsKey(as)) { @@ -160,9 +167,14 @@ class Container { } _factories[as] = f; + return f; } - void registerSingleton(T object, {Type as}) { + /// Registers a singleton. Any attempt to resolve the + /// type within *this* container will return [object]. + /// + /// Returns [object]. + T registerSingleton(T object, {Type as}) { as ??= T == dynamic ? as : T; if (_singletons.containsKey(as ?? object.runtimeType)) { @@ -171,6 +183,7 @@ class Container { } _singletons[as ?? object.runtimeType] = object; + return object; } /// Finds a named singleton. @@ -194,11 +207,12 @@ class Container { /// /// Note that this is not related to type-based injections, and exists as a mechanism /// to enable injecting multiple instances of a type within the same container hierarchy. - void registerNamedSingleton(String name, T object) { + T registerNamedSingleton(String name, T object) { if (_namedSingletons.containsKey(name)) { throw StateError('This container already has a singleton named "$name".'); } _namedSingletons[name] = object; + return object; } }