registerFactory<T> method

void registerFactory<T>(
  1. T factory(), {
  2. String? moduleId,
  3. bool allowOverwrite = false,
})

Registers a Factory.

A new instance is created every time get is called by executing the factory function.

factory A function that creates a new instance cada vez que se solicita. moduleId Optional ID of the module that owns this registration. allowOverwrite If true, allows replacing an existing registration.

Implementation

void registerFactory<T>(
  T Function() factory, {
  String? moduleId,
  bool allowOverwrite = false,
}) {
  if (_registrations.containsKey(T) && !allowOverwrite) {
    AirLogger.warning(
      'Dependency already registered',
      context: {'type': T.toString()},
    );
    throw DependencyAlreadyRegisteredException(T);
  }

  _registrations[T] = _FactoryDependency<T>(factory);
  _registrationOwners[T] = moduleId;

  AirLogger.debug(
    'Registered Factory',
    context: {'type': T.toString(), 'module': moduleId},
  );
}