From 0915c48054b66a703e8fc37b80562ee1c666b6a8 Mon Sep 17 00:00:00 2001 From: Patrick Stewart Date: Wed, 2 Oct 2024 19:17:17 -0700 Subject: [PATCH] Add: support for registerTransient, registerScoped and registerConstant --- .../container/lib/src/container.dart | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core/container/container/lib/src/container.dart b/core/container/container/lib/src/container.dart index 5fb963a5..5fd7576d 100644 --- a/core/container/container/lib/src/container.dart +++ b/core/container/container/lib/src/container.dart @@ -236,4 +236,28 @@ class Container { _namedSingletons[name] = object; return object; } + + /// Register a scoped instance (new instance per scope). + void registerScoped(T Function(Container) factory) { + // Implement scoped behavior + // This is a simplified version; you might need to implement + // proper scoping mechanism based on your requirements + var scope = {}; + registerFactory((c) { + if (!scope.containsKey(T)) { + scope[T] = factory(c); + } + return scope[T] as T; + }); + } + + /// Register a transient instance (new instance every time). + void registerTransient(T Function(Container) factory) { + registerFactory((c) => factory(c)); + } + + /// Register a constant value. + void registerConstant(T value) { + registerFactory((c) => value); + } }