registerAlias method

  1. @override
void registerAlias(
  1. String name,
  2. String alias
)
override

Registers an alias for an existing name in the registry.

Associates a new alias with an existing name, allowing the object registered under name to be accessed using the alias as well.

Parameters:

  • name: The primary name that already exists in the registry
  • alias: The alternative name to register for the primary name

Throws:

Example:

final registry = MyAliasRegistry();

// First register a primary name
registry.registerPrimary('emailService');

// Then register aliases for it
registry.registerAlias('emailService', 'mailService');
registry.registerAlias('emailService', 'smtpService');

// Now the service can be accessed by any of these names
final service1 = registry.get('emailService');
final service2 = registry.get('mailService'); // Same as service1
final service3 = registry.get('smtpService'); // Same as service1

Implementation

@override
void registerAlias(String name, String alias) {
  if (name.isEmpty) {
    throw IllegalArgumentException("'name' must not be empty");
  }
  if (alias.isEmpty) {
    throw IllegalArgumentException("'alias' must not be empty");
  }

  return synchronized(this, () {
    final aliasMap = _getAliasMap();
    final aliasNames = _getAliasNames();

    if (alias == name) {
      aliasMap.remove(alias);
      aliasNames.remove(alias);
      // Log debug: Alias definition ignored since it points to same name
      return;
    }

    final registeredName = aliasMap[alias];
    if (registeredName != null) {
      if (registeredName == name) {
        // An existing alias - no need to re-register
        return;
      }
      if (!allowAliasOverriding()) {
        throw IllegalStateException("Cannot define alias '$alias' for name '$name': It is already registered for name '$registeredName'.");
      }
      // Log debug: Overriding alias definition
    }

    checkForAliasCircle(name, alias);
    aliasMap[alias] = name;
    aliasNames.add(alias);
    // Log trace: Alias definition registered
  });
}