Libraries
    Preparing search index...

    Enumeration ServiceLifetime

    Defines the lifetime of a service within the dependency injection container.

    The lifetime determines when a new instance is created versus when a cached instance is returned.

    import { ServiceCollection, ServiceLifetime } from '@cleverbrush/di';
    import { object, string } from '@cleverbrush/schema';

    const ILogger = object({ info: func() });

    const services = new ServiceCollection();
    // Equivalent to services.addSingleton(ILogger, ...)
    services.add(ILogger, () => console, ServiceLifetime.Singleton);
    Index

    Enumeration Members

    Enumeration Members

    Scoped: "Scoped"

    One instance is created per ServiceScope. The same instance is returned for every resolution within that scope.

    Use for services that should be shared within a single unit of work (e.g. an HTTP request) but isolated between units.

    Singleton: "Singleton"

    One instance is created for the entire lifetime of the ServiceProvider. All subsequent resolutions return the same instance.

    Use for expensive-to-create or truly global services (loggers, configuration, connection pools).

    Transient: "Transient"

    A new instance is created every time the service is resolved.

    Use for lightweight, stateless services.