Add: support for registerTransient, registerScoped and registerConstant

This commit is contained in:
Patrick Stewart 2024-10-02 19:17:17 -07:00
parent 11c12fd590
commit 0915c48054

View file

@ -236,4 +236,28 @@ class Container {
_namedSingletons[name] = object;
return object;
}
/// Register a scoped instance (new instance per scope).
void registerScoped<T>(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<T>((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>(T Function(Container) factory) {
registerFactory<T>((c) => factory(c));
}
/// Register a constant value.
void registerConstant<T>(T value) {
registerFactory<T>((c) => value);
}
}