Add: support for registerTransient, registerScoped and registerConstant
This commit is contained in:
parent
11c12fd590
commit
0915c48054
1 changed files with 24 additions and 0 deletions
|
@ -236,4 +236,28 @@ class Container {
|
||||||
_namedSingletons[name] = object;
|
_namedSingletons[name] = object;
|
||||||
return 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue