Skip to content

Function: Scoped() ​

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

Make the class scoped: the container creates one instance per scope (an entrypoint creates a scope for each request), shared by the objects of that scope and destroyed with it; outside a scope, the container registering the class shares one instance. Shorthand of @Lifetime('SCOPED', lock); the default of Provider.

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 {Scoped} from 'lakutata/decorator/di'

//The objects handling the same request share its transaction
@Scoped()
export class RequestTransaction extends Provider {
    public readonly startedAt: number = Date.now()
}

Released under the MIT License.