spot<T> method
Resolve and return an instance of type T.
Resolution order:
- Check local registry
- If not found, check parent (if exists)
- Continue up the chain until found or throw exception
Type Parameters:
T: The type to resolve (must be registered in this scope or parent)
Parameters:
name: Optional name qualifier for named instances
Returns: Instance of type T
Throws:
- SpotException if
Tis not registered in this scope or any parent - SpotException if circular dependency detected
- SpotException if trying to resolve async singleton synchronously
Implementation
T spot<T>({String? name}) {
final key = SpotKey<T>(T, name);
// Fast path: check singleton cache first
if (_singletonCache.containsKey(key)) {
if (logging) log.v('Cache hit in scope for $key');
return _singletonCache[key] as T;
}
// Check local registry
if (registry.containsKey(key)) {
return _resolveLocal<T>(key);
}
// Fall back to parent
if (parent != null) {
if (logging) log.v('Falling back to parent for $key');
return parent!.spot<T>(name: name);
}
// Not found in this scope or any parent
final registeredTypes = _getAllRegisteredKeys().map((k) => k.toString()).join(', ');
throw SpotException(
'Type $key is not registered in this scope or any parent scope.\n'
'Registered in this scope: ${registry.keys.map((k) => k.toString()).join(', ')}\n'
'All registered types: ${registeredTypes.isNotEmpty ? registeredTypes : '(none)'}'
);
}