resolve<T> method

T resolve<T>({
  1. String? name,
})

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 instance = _scopedFactories[key]!(this);
    _instances[key] = instance;
    return instance as T;
  }

  if (_factories.containsKey(key)) {
    final instance = _factories[key]!(this);
    return instance as T;
  }

  if (parent != null) {
    return parent!.resolve<T>(name: name);
  }

  throw Exception('Service $key not registered in this container or any of its parents.');
}