resolve<T> method
Resolves an instance of type T.
Searches local instances, then singletons, then factories. If not found, delegates to the parent container. Throws if not found anywhere.
Implementation
T resolve<T>({String? name}) {
final key = _key<T>(name);
if (_instances.containsKey(key)) return _instances[key] as T;
if (_singletons.containsKey(key)) return _singletons[key] as T;
if (_scopedFactories.containsKey(key)) {
final res = _scopedFactories[key]!(this);
_instances[key] = res;
return res as T;
}
if (_factories.containsKey(key)) {
final res = _factories[key]!(this);
return res as T;
}
final p = parent;
if (p != null) {
return p.resolve<T>(name: name);
}
throw Exception('Service $key not registered in this container or any of its parents.');
}