replace<T extends Object> static method

  1. @visibleForTesting
void replace<T extends Object>(
  1. T instance, {
  2. String? key,
})

Replaces an instance in all modules that have registered it.

This method searches through all registered modules and replaces the instance in any module that has registered a dependency of type T. This is useful when you don't know which module owns the dependency, but it can be slower than replaceFromModule since it needs to check all modules.

Example:

EasyDI.replace<UserRepository>(mockUserRepository);

Implementation

@visibleForTesting
static void replace<T extends Object>(T instance, {String? key}) {
  for (final module in _modules.values) {
    if (module.injector!.isAdded<T>()) {
      module.injector!.replace<T>(instance, key: key);
    }
  }
}