register<T> method

void register<T>(
  1. Factory<T> factory, {
  2. bool override = false,
  3. String dependencyName = "",
})

Registers a dependency that will be created with the provided Factory. See Factory.provider or Factory.singleton. You can also create your custom factory by implementing Factory.

Overrides dependencies with the same signature when override is true. Uses dependencyName to differentiate between dependencies that have the same type.

The signature of a dependency consists of T and the optional dependencyName.

abstract class UserService {
  void login(String username, String password);
}

class UserServiceImpl implements UserService {
  void login(String username, String password) {
    .....
    .....
  }
}

injector.register(Factory.singleton(() => UserServiceImpl()));

Then getting the registered dependency:

injector.get<UserService>();

Implementation

void register<T>(Factory<T> factory,
    {bool override = false, String dependencyName = ""}) {
  _checkValidation<T>();

  final identity = _getIdentity<T>(dependencyName);

  if (!override) {
    _checkForDuplicates<T>(identity);
  }

  _factoryMap[identity] = factory;
}