put<T> method
Register a dependency in this scope
Implementation
T put<T>(T instance, {String? tag, bool? isPermanent}) {
if (_disposed) {
throw Exception('Cannot register in a disposed scope: $name');
}
// Smart default: ZenService instances default to permanent (same as Zen.put)
final permanent = isPermanent ?? (instance is ZenService ? true : false);
// Auto-initialize ZenController instances
if (instance is ZenController) {
_initializeController(instance);
}
// Auto-initialize ZenService instances via lifecycle manager (same as Zen.put)
else if (instance is ZenService) {
_lifecycleManager.initializeService(instance);
}
if (tag != null) {
// Check if we're replacing a dependency
final oldInstance = _taggedBindings[tag];
if (oldInstance != null) {
if (ZenConfig.enableDebugLogs) {
ZenLogger.logWarning('Replacing existing dependency with tag: $tag');
}
// Dispose old controller if it's a ZenController
if (oldInstance is ZenController && !oldInstance.isDisposed) {
oldInstance.dispose();
}
}
// Store by tag
_taggedBindings[tag] = instance;
// Track by type for faster lookups
_typeToTags.putIfAbsent(T, () => <String>{}).add(tag);
// Set initial use count
final key = _getDependencyKey(T, tag);
_useCount[key] = permanent ? -1 : 0;
} else {
// Check if we're replacing a dependency
final oldInstance = _typeBindings[T];
if (oldInstance != null) {
if (ZenConfig.enableDebugLogs) {
ZenLogger.logWarning('Replacing existing dependency of type: $T');
}
// Dispose old controller if it's a ZenController
if (oldInstance is ZenController && !oldInstance.isDisposed) {
oldInstance.dispose();
}
}
// Store by type
_typeBindings[T] = instance;
// Set initial use count
final key = _getDependencyKey(T, null);
_useCount[key] = permanent ? -1 : 0;
}
if (ZenConfig.enableDebugLogs) {
ZenLogger.logDebug(
'Registered $T${tag != null ? ' with tag $tag' : ''} (${permanent ? 'permanent' : 'temporary'})');
}
return instance;
}