Skip to content

Function: Transient() ​

Transient<ClassConstructor>(lock?): ClassDecorator<ClassConstructor>

Make the class transient: the container creates a new instance on each request (each getObject() and each injection), never cached. Shorthand of @Lifetime('TRANSIENT', lock); the default of BaseObject.

Type Parameters ​

ClassConstructor ​

ClassConstructor extends typeof BaseObject

Parameters ​

lock? ​

boolean

Whether the subclasses are forbidden to change the lifetime (they may still declare the same one). Default false.

Returns ​

ClassDecorator<ClassConstructor>

The class decorator.

Throws ​

When the class is defined, if a parent class locked another lifetime.

Example ​

typescript
import {Provider} from 'lakutata'
import {Transient} from 'lakutata/decorator/di'

//Each user of the builder gets its own, with its own state
@Transient()
export class QueryBuilder extends Provider {
    protected readonly clauses: string[] = []

    public where(clause: string): this {
        this.clauses.push(clause)
        return this
    }
}

Released under the MIT License.