From f9fae921cc0b0aacf2e1d8f99f9b5ba8eefaa9d0 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 22 Oct 2018 11:21:58 -0400 Subject: [PATCH] registerLazySingleton --- angel_container/lib/src/container.dart | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/angel_container/lib/src/container.dart b/angel_container/lib/src/container.dart index 38ecf942..07bfd87f 100644 --- a/angel_container/lib/src/container.dart +++ b/angel_container/lib/src/container.dart @@ -92,6 +92,20 @@ 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( + (container) { + var r = f(container); + container.registerSingleton(r, as: as); + return r; + }, + as: as, + ); + } + void registerFactory(T Function(Container) f, {Type as}) { as ??= T;