# addSingleton method

1. @protected

Future<void> addSingleton(

1. String name, {
2. ObjectHolder<Object>? object,
3. ObjectFactory<Object>? factory,

})
inherited

Add the given singleton object to the singleton cache of this factory.

This method registers a fully initialized singleton instance and cleans up any intermediate state (factories, early references).

`name`: The name of the pod to register `qualifiedName`: The fully qualified name of the pod to register `singleton`: The fully initialized singleton instance

Example:

    final userService = UserService();
    // Perform dependency injection and initialization
    injectDependencies(userService);
    initializePod(userService);

    registry.addSingleton('userService', userService);

Add the given singleton factory for building the specified singleton if necessary.

This method registers a factory function that can create the singleton instance when needed. Useful for lazy initialization and circular dependency resolution.

`name`: The name of the pod to register a factory for `singletonFactory`: The factory function that creates the singleton instance

Example:

    registry.addSingletonFactory('dataSource', () {
      final dataSource = DataSource();
      dataSource.initialize();
      return dataSource;
    });

## Implementation

    @protected
    Future<void> addSingleton(String name, {ObjectHolder<Object>? object, ObjectFactory<Object>? factory}) async {
      if(object == null && factory == null) {
        throw IllegalArgumentException("At least one of object or factory must be provided");
      }

      if(object != null) {
        return synchronized(_singletons, () {
          _singletons[name] = object;
          _singletonFactories.remove(name);
          _earlySingletons.remove(name);
          _registeredSingletons.add(name);

          final obj = object.getValue();

          final callback = singletonCallbacks[name];
          if(callback != null) {
            callback(obj);
          }

          if(obj is DisposablePod && !_disposablePods.containsKey(name)) {
            _disposablePods[name] = object;
          }

          final type = object.getType();
          if (type != null) {
            _singletonTypes[name] = type;
          }
        });
      } else if(factory != null) {
        return synchronized(_singletons, () async {
          _singletonFactories[name] = factory;
          _earlySingletons.remove(name);
          _registeredSingletons.add(name);
        });
      }
    }